69.Sqrt(x)
2015-06-26 16:01
239 查看
Implement
Compute and return the square root of x.
灵活的使用了二分查找。。。
太机智了
int sqrt(int x).
Compute and return the square root of x.
灵活的使用了二分查找。。。
太机智了
class Solution { public: int mySqrt(int x) { int left = 1; int right = x >>1; int mid; int lastMid; if(x < 2) return x; while(left <= right) { mid = (left + right) >>1; if(x / mid > mid) { left = mid + 1; lastMid = mid; } else if( x / mid < mid) { right = mid - 1; } else return mid; } return lastMid; } };
相关文章推荐
- 有道词典的简单实现
- JAVA并发编程
- 【第四篇章-android平台MediaCodec】编解码逻辑认识
- getcwd: cannot access parent directories的解决方法
- java java启动方式
- Hibernate get和load区别
- 以system权限修改注册表的方法
- php使用workerman自定义协议
- 算法复杂度分析
- 大型网站技术架构
- web总结
- CocoaHTTPServer-master
- Struts2 XML配置详解
- JAVA/Android 读写文件,避免中文乱码 FileWriter乱码
- iOS开发辅助工具-插件集成管理工具-Alcatraz的安装和使用
- Android requires compiler compliance level 5.0 or 6.0. Found '1.8' instead. Please use Android Tools>Fix project Properties.
- Excel 中使用SQL 语句查询数据(八)-----用Group by 进行分组统计
- 《Windows服务器配置与管理》搭建DNS服务器
- 常见手机分辨率
- 控制器之间传值,逆传