LeetCode 69. Sqrt(x)
来源:互联网 发布:淘宝老顾客回购率 编辑:程序博客网 时间:2024/05/29 02:58
Implement int sqrt(int x)
.
Compute and return the square root of x.
实现开方功能,第一个想到的就是二分法,注意的是,为了防止溢出,需要用除法比较而不是乘法
public class Solution { public int mySqrt(int x) { if(x<2)return x; int r = x/2+1; int l = 1; int m = (l+r)/2; while(l<=r){ if(m<=x/m&&(m+1)>x/(m+1)){ return m; } if(m>x/m){ r = m-1; m = (l+r)/2; } else{ l = m+1; m = (l+r)/2; } } return m; }}
阅读全文
0 0
- LeetCode 69. Sqrt(x)
- LeetCode 69.Sqrt(x)
- LeetCode --- 69. Sqrt(x)
- [Leetcode] 69. Sqrt(x)
- [leetcode] 69.Sqrt(x)
- Leetcode-69.Sqrt(x)
- leetcode--69.sqrt(x)
- LeetCode 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)
- 69. Sqrt(x) LeetCode
- [leetcode] 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- LeetCode 69. Sqrt(x)
- LeetCode 69. Sqrt(x)
- leetcode-69. Sqrt(x)
- [leetcode]69. Sqrt(x)
- LeetCode-69.Sqrt(x)
- C++程序员学Java系列之十:Java中的循环while,for,do while,猜数字案例
- No tests found with test runner JUnit4
- Golang 优化之路——临时对象池
- jmeter
- hive------select语法介绍
- LeetCode 69. Sqrt(x)
- Apriori的Python实现
- java基础入门学习
- Eclipse创建Maven项目报错的解决
- IRP结构
- 使用了未定义的类_解决方法
- 最全原生AJAX请求步骤
- 在父页面处理用户控件的事件
- hive------join语法介绍