69. Sqrt(x)
来源:互联网 发布:北京企业全网营销优化 编辑:程序博客网 时间:2024/06/08 19:26
Implement int sqrt(int x).
Compute and return the square root of x.
思路: sqrt(x)肯定是小于x的大于0的数,所以可以用二分查找。注意最后返回high的值是说如果x没有正数的平方根取最接近于小于mid的那个数。注意mid要用long,防止mid*mid越界。
http://www.cnblogs.com/springfor/p/3857772.html
class Solution {public: int mySqrt(int x) { int low = 0; int high = x; while(low <= high) //不是< { long mid = long((low + high)/2); if(mid * mid > x) { high = (int)(mid) -1; } else if (mid * mid < x) { low = int(mid) + 1; } else { return (int)mid; } } return high; }};
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)
- mac 安装boost 库
- 1113. Integer Set Partition (25)-PAT甲级真题
- Java中的空指针异常
- 深入理解JVM(三)——垃圾收集策略详解
- 深入理解JVM(四)——对象内存的分配策略
- 69. Sqrt(x)
- 深入理解JVM(五)——HotSpot垃圾收集器详解
- Codeforces Round #368 C Pythagorean Triples
- LintCode:判断数独是否合法
- 深入理解JVM(六)——JVM性能调优实战
- Write the code. Change the world.
- ResultSet的理解
- 深入理解JVM(七)——Class文件结构
- #3 longest-substring-without-repeating-characters