Leetcode 69. Sqrt(x)
2018-02-04 06:44
357 查看
原题:
Implement
Compute and return the square root of x.
x is guaranteed to be a non-negative integer.
Example 1:
Example 2:
解决方法:
从该数本身开始,找到第一个平方不小于目标的数即是所求解。
代码:
Implement
int sqrt(int x).
Compute and return the square root of x.
x is guaranteed to be a non-negative integer.
Example 1:
Input: 4 Output: 2
Example 2:
Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated.
解决方法:
从该数本身开始,找到第一个平方不小于目标的数即是所求解。
代码:
int mySqrt(int x) { if(x < 0) return -1; if (x <=1) return x; long i = x; while(i*i > x){ i = ((i + x/i) >> 1); } return i; }
相关文章推荐
- [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 - 50/69/367/633 - Pow(x, n)、Sqrt(x)
- leetcode 69. Sqrt(x)
- 【LeetCode】69. Sqrt(x) 解题报告
- 【LeetCode】69. Sqrt(x) (2 solutions)
- [Leetcode] 69. Sqrt(x)
- LeetCode_69---Sqrt(x)
- LeetCode 第 69 题 (Sqrt(x))
- LeetCode(69) Sqrt(x)
- leetcode 69 Sqrt(x)
- (java)leetcode-69:Sqrt(x)
- LeetCode---(69)Sqrt(x)