69. Sqrt(x)
来源:互联网 发布:淘宝小北名表靠谱吗 编辑:程序博客网 时间:2024/06/05 06:02
Implement int sqrt(int x)
.
Compute and return the square root of x.
这个题就是实现sqrt方法,使用牛顿迭代法,采用切线的方式接近所求值比二分查找要快很多。
class Solution {public:int mySqrt(int x) { if(!x) return x;float i = x ;float last;while (abs(i-last) > 0.00000001) { last = i;i = (i + x / i) / 2;}int j =i;if(j * j > x) j--; return j; }};就是最后得到i的处理花了些时间,看了网上的写法补上了最后处理i的几行,得到正确结果。
0 0
- LeetCode 69. Sqrt(x)
- LeetCode 69.Sqrt(x)
- LeetCode --- 69. Sqrt(x)
- [Leetcode] 69. Sqrt(x)
- [leetcode] 69.Sqrt(x)
- 69.Sqrt(x)
- 69. Sqrt(x)
- Leetcode-69.Sqrt(x)
- leetcode--69.sqrt(x)
- 69. Sqrt(x)
- LeetCode 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)
- 69. Sqrt(x) LeetCode
- 69. Sqrt(x)
- 69. Sqrt(x)
- [leetcode] 69. Sqrt(x)
- 69. Sqrt(x)
- PTA 5-34 通讯录的录入与显示
- Java泛型中<T extends Comparable<? super T>>是什么意思
- 李笑来:这些年我的读书经验
- 八种创建等高列布局
- 译文:使用MVC5的Entity Framework 6入门(十)——为ASP.NET MVC应用程序处理并发
- 69. Sqrt(x)
- MyBatis之Base64加密数据源
- E
- 把Centos7的GUI桌面更换为命令行模式
- javademo2
- 关于linux下安装jkd,配置环境变量遇到的问题,及其解决办法
- 常见算法之桶排序
- bzoj #1854 游戏(二分图匹配)
- JAVA局部内部类