您的位置:首页 > 其它

LeetCode-Sqrt(x)

2014-08-10 21:54 309 查看
Implement 
int sqrt(int x)
.

Compute and return the square root of x.
Solution:
Code:

<span style="font-size:14px;">class Solution {
public:
int sqrt(int x) {
if (x == 0 || x == 1) return x;
unsigned long long begin = 0;
unsigned long long end = (x+1)/2;
unsigned long long mid;
unsigned long long temp;
while (begin < end) {
mid = (begin+end)/2;
temp = mid*mid;
if (temp == x) return mid;
else if (temp > x) end = mid-1;
else begin = mid+1;
}
temp = end*end;
if (temp > x)
return end-1;
else
return end;
}
};</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode Sqrtx