【leetcode】69-Sqrt(x)

problem

Sqrt(x)

code

class Solution {
public:
    int mySqrt(int x) {// x/b=b
        long long res = x;//
        while(res*res > x)
        {
            res = (res + x/res) / 2;//
        }
        return res;
    }
};

重点在于理解怎么计算平方根。

The key point is the average result is calculate by "ans = (ans + x / ans) / 2";

 

参考

1.leetcode_sqrt(x);

posted on 2018-11-22 09:23  鹅要长大  阅读(375)  评论(0编辑  收藏  举报

导航