您的位置:首页 > 其它

Leetcode134: Sqrt(x)

2015-11-02 17:12 375 查看
Implement
int sqrt(int x)
.

Compute and return the square root of x.

class Solution {
public:
int mySqrt(int x) {
if(x<=1)
return x;
int l = 1;
int r = x/2;
while(l<=r)
{
int mid = (r+l)/2;
if(mid== x/mid)
return mid;
else if(mid > x/mid)
r = mid-1;
else
l = mid+1;
}
return r;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: