求取平方根
class Solution {
public:
int sqrt(int x) {
if(x<=0){
return 0;
}
long res=x;
while(res*res>x){
res=(res+x/res)/2; //要先加
}
return res;
}
};
public class Solution {
public int sqrt(int x) {
int low = 0;
int high = x;
while (low <= high) {
long mid = (low + high) / 2;
if(mid * mid == x) return (int)mid;
else if(mid * mid < x) low = (int)(mid + 1);
else high = (int)(mid - 1);
}
return high;
}
}