LeetCode : Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

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

posted on 2017-05-02 23:15  gechen  阅读(68)  评论(0编辑  收藏  举报

导航