https://leetcode.com/problems/sqrtx/description/
Solution 1.
int mySqrt(int x) {
if(x == 0) return 0;
int n = 0;
for(int m = 1<<30; m!=0; m>>=1){
int r = n|m;
while(r>x/r) {
m >>= 1;
r = n|m;
}
n |= m;
}
return n;
}
Solution 2
int mySqrt(int x) {
if(x == 0) return 0;
double y = log10(x)/2.;
return (int) pow(10.,y);
}
No comments:
Post a Comment