[LeetCode]69 Sqrt(x)
2015-01-04 11:00
411 查看
https://oj.leetcode.com/problems/sqrtx/
http://blog.csdn.net/linhuanmars/article/details/20089131
http://blog.csdn.net/linhuanmars/article/details/20089131
public class Solution { public int sqrt(int x) { // Assume x >= 0 if (x == 0) return 0; return s(x, 0L, (long)x); } private int calc(int x, long low, long high) { // Why using long. // is because we might calculate (MAX_VALUE / 2 + somenumber) ^ 2 if (low > high) { // This is the stop point. return (int)high; } long mid = (low + high) / 2; long y = mid * mid; if (x == y) { return (int)mid; } else if (x > y) { return calc(x, mid + 1, high); } else { return calc(x, low, mid - 1); } } }
相关文章推荐
- LeetCode - 50/69/367/633 - Pow(x, n)、Sqrt(x)
- leetcode ----69. Sqrt(x)
- leetcode_69题——Sqrt(x)(二分查找,还有个溢出问题的研究)
- [Leetcode 69, Medium] Sqrt(x)
- 【LeetCode 69】Sqrt(x)
- leetcode oj java 69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)
- LeetCode 69. Sqrt(x)
- Leetcode 69. Sqrt(x)
- Leetcode 69. Sqrt(x)
- leetcode69: Sqrt(x)
- Leetcode——69. Sqrt(x)
- (java)leetcode-69:Sqrt(x)
- [Leetcode] 69. Sqrt(x)
- LeetCode69 Sqrt(x)
- leetcode 69. Sqrt(x)
- leetcode解题之69.Sqrt(x) & 367. Valid Perfect Square Java版 (求一个数的平方根)
- Leetcode 69 Sqrt(x) 二分查找(二分答案)
- leetcode69---Sqrt(x)(求x的平方根)