LeetCode 69. Sqrt(x),求根算法
2016-03-27 15:28
453 查看
69. Sqrt(x)
Implement
Compute and return the square root of x.
Subscribe to see which companies asked this question
这道题要找x的平方根,x的平方根肯定小于x/2。要在[1,x/2]有序序列当中找一个数,用二分法:
Implement
int sqrt(int x).
Compute and return the square root of x.
Subscribe to see which companies asked this question
这道题要找x的平方根,x的平方根肯定小于x/2。要在[1,x/2]有序序列当中找一个数,用二分法:
public int mySqrt(int x) { long high = (x / 2) + 1; long low = 0; while (high >= low) { long mid = (high + low) / 2; if (mid * mid == x) return (int)mid; else if (mid * mid > x) high = mid - 1; else low = mid + 1; } return (int)high; }
相关文章推荐
- Java接受输入参数
- 第四次上机实践项目-项目3-时间类-(1)
- FZU 1055 赋值问题
- AFNetworking2.x 使用过程中遇到的问题
- 使用awrextr.sql导出awr原始数据
- Apache Commons工具集简介
- C#-GC基础(待补充)
- Linux基础--系统启动流程
- 标准C++中的string类的用法总结
- 推荐 10 个超棒的 CSS3 代码生成工具
- 合并多个有序链表
- 【Java解惑】表达式问题
- Java多线程系列--“JUC集合”08之 LinkedBlockingQueue
- hdoj decimal system 2106 (进制转换) 水
- 第5周-项目1-三角形类锥形(3)
- 给linux移植Ethercat笔记
- 5.伪静态(2) --- rewrite 机制
- c++第二次实验-1
- ViewPager切换动画——PageTransformer
- 多线程