Implement int sqrt(int x).
Compute and return the square root of x.
class Solution {
public:
int mySqrt(int x) {
unsigned long long begin = 0;
unsigned long long end = (x+1)/2;
unsigned long long mid;
unsigned long long tmp;
while(begin < end){
mid = begin + (end - begin)/2;
tmp = mid * mid;
if(tmp == x)return mid;
else if(tmp > x) end = mid - 1;
else begin = mid + 1;
}
tmp = end*end;
if(tmp > x){
return end-1;
}else{
return end;
}
}
};
69. Sqrt(x)
最新推荐文章于 2022-01-10 17:10:52 发布