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

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;
    }
};

 

相关文章:

  • 2021-10-06
  • 2022-12-23
  • 2022-01-08
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2021-06-15
猜你喜欢
  • 2021-08-20
  • 2021-12-18
  • 2021-06-21
  • 2021-10-23
  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案