1.题目69:实现一个数的开根
解题一:
class Solution {
int mySqrt(int x) {
int m=x/2;
if(x==0)
return 0;
long i=1;
for(;i<=m;i++)
{
if(i*i<=x&&(i+1)*(i+1)>x)
break;
}
return (int)i;
}
};
ps:这里需要一次类型转换,long to int
在java中,取值范围小的自动会转成取值范围大的类型,但是对于取值范围大到小的转换需要强制转换,否则会出错。
取值范围:byte<char<int(四个字节)<long(8个字节)<float(四个字节)<double(8个字节)