二分查找——sqrtx
2016-05-28 15:53
465 查看
题目描述
Implementint sqrt(int x).Compute and return the square root ofx.
public class Solution { public int sqrt(int x) { if(x <= 0) return 0; int start=1; int end=x/2+1;//end*end=x*x/4+x+1>x,提快速度; while(start<end-1) { int mid=start+(end-start)/2; if(mid == x/mid)//若用mid*mid 会越界。 return mid; else if(mid > x/mid) end=mid; else start=mid; } return start; } }
相关文章推荐
- phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)
- redis cluster install
- 编写category时的便利宏(用于解决category方法从静态库中加载需要特别设置的问题)
- phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)
- 安装第三方模块方法和requests
- 1039. 到底买不买(20)
- android 颜色配置
- jquery ajax cache的问题
- 成长日记
- Delphi下生成Map文件方法
- zookeeper install
- char 与 string对象相互转换
- 统计学习理论
- PHP接口的一个案例
- cp 拷贝,略过不想拷贝的目录
- android AsyncTask介绍
- 未能正确加载“RoslynPackage”包
- 个人报告10
- dup2()功能实现
- Myeclipse10+Egit+bitbucket实现版本控制