leetcode Sqrt(x) 求开方
2017-09-20 16:36
316 查看
Implement int sqrt(int x).
Compute and return the square root of x.
直接暴力搜索会超时,所以使用二分法的思想来做,时间复杂度o(log x)。
Compute and return the square root of x.
class Solution { public: int mySqrt(int x) { if (x == 0) return 0; int left = 1, right = INT_MAX; while(1) { int mid =left + (right - left) / 2; if (mid > x / mid) right = mid - 1; else { if (mid + 1 > x / (mid + 1)) return mid; left = mid + 1; } } } };
直接暴力搜索会超时,所以使用二分法的思想来做,时间复杂度o(log x)。
相关文章推荐
- 每天一道LeetCode-----重新实现开方运算sqrt(x),只返回整数部分即可
- leetcode 69. Sqrt(x)(C语言,牛顿迭代法求开方问题)46
- leetcode:Sqrt(x) 牛顿迭代法求整数开方
- LeetCode:Sqrt(x)
- [Leetcode] sqrt 开根号
- LeetCode *** 69. Sqrt(x) 牛顿迭代法
- Leetcode NO.69 Sqrt(x)
- LeetCode 69. Sqrt(x)
- Sqrt(x) - LeetCode 69
- LeetCode - 69. Sqrt(x)
- LeetCode: Sqrt(x)
- LeetCode题解:Sqrt(x)
- 69. Sqrt(x) LeetCode
- 【LeetCode】69. Sqrt(x)
- leetcode 69: Sqrt(x)
- LeetCode | 69. Sqrt(x)
- Leetcode 69. Sqrt(x) (Medium) (cpp)
- Sqrt(x) leetcode
- Leetcode 69. Sqrt(x)
- [leetcode] 69. Sqrt(x)