美文网首页
367. Valid Perfect Square

367. Valid Perfect Square

作者: SilentDawn | 来源:发表于2018-10-11 21:41 被阅读0次

Problem

Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.

Example

Input: 16
Output: true
Input: 14
Output: false

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    bool isPerfectSquare(int num) {
        long temp = num;
        while (temp*temp > num)
            temp = (temp + num/temp) / 2;
        return temp*temp == num;
    }
};

Result

image.png

相关文章

网友评论

      本文标题:367. Valid Perfect Square

      本文链接:https://www.haomeiwen.com/subject/acpiaftx.html