69. Sqrt(x)
2016-07-19 15:46
295 查看
Implement
Compute and return the square root of x.
暴力法
public static int mySqrt(int x)
{
if(x<=0)
return x;
if(x==1)
return 1;
int i=1;
for(;i<=46340;i++)
if(i*i>x)
break;
return i-1;
}
二分查找
public int mySqrt(int x) {
if(x <= 1) return x;
int left = 1, right = x;
while(left < right) {
int mid = left + (right - left) / 2;
if(mid <= x / mid) {
left = mid + 1;
} else {
right = mid;
}
}
return left - 1;
}
int sqrt(int x).
Compute and return the square root of x.
暴力法
public static int mySqrt(int x)
{
if(x<=0)
return x;
if(x==1)
return 1;
int i=1;
for(;i<=46340;i++)
if(i*i>x)
break;
return i-1;
}
二分查找
public int mySqrt(int x) {
if(x <= 1) return x;
int left = 1, right = x;
while(left < right) {
int mid = left + (right - left) / 2;
if(mid <= x / mid) {
left = mid + 1;
} else {
right = mid;
}
}
return left - 1;
}
相关文章推荐
- volatile常量在同步的使用详解
- 八、Python正则表达式
- 【Heritrix 04】heritrix.properties+java.util.logging.Logger
- 343. Integer Break
- Android数据绑定框架DataBinding用法
- 7.jvm之认证和签名
- IE只读的input,按退格键的bug
- 数学有什么用处?看完后恍然大悟!
- deepin配置vsftp(仅配置篇)
- CodeForces 347B Fixed Points
- 将excel转换成pdf格式的方法
- com/android/dx/command/dexer/Main : Unsupported major.minor version 52.0
- android 数据双向绑定学习笔记
- 颜色表示:HSB,Lab,RGB,CMYK与十六进制表示法
- JS对象及其方法-数组
- HDU5489 Removed Interval (LIS变形)
- Android Touch事件传递
- HTTP协议(转自:小坦克博客)
- Android Ant 自动打包 修改包名,修改logo,修改名字,修改配置
- Websocket 协议解析