valid-perfect-square

这道题目的循环里面的那个递推的式子很巧妙,能够帮助循环快速收敛。

class Solution {
public:
    bool isPerfectSquare(int num) {
        // refer to
        // https://leetcode.com/discuss/110671/3-4-short-lines-integer-newton-most-languages
        int x = num;
        while ((long long)x * x > num) {
            // below statement is important
            x = (x + num / x) / 2;
        }
        return (long long)x * x == num;
    }
};
原文地址:https://www.cnblogs.com/charlesblc/p/5620222.html