Implement int sqrt(int x).
Compute and return the square root of x.
注意:越界问题
class Solution {
public:
int sqrt(int x) {
if(x<0) return -1;
if(x==0||x==1) return x;
int start,end;
long long mid; //!!!!!!int may cause overflow
start=1,end=x>>1;
while(end>=start){
mid=(start+end)>>1;
if(mid*mid<=x && (mid+1)*(mid+1)>x)
return mid;
else if(mid*mid>x)
end=mid-1;
else
start=mid+1;
}//while
}
};