leetcode Sqrt(x) Binary Search
来源:互联网 发布:美国非农数据公布网址 编辑:程序博客网 时间:2024/04/28 12:54
Pay attention to that mid * mid may be larger than int. So long long is enough.
class Solution { public: int sqrt(int x) { // Note: The Solution object is instantiated only once and is reused by each test case. int l = 0, r = x; while (l <= r) { int mid = (l + r) / 2; long long mid2 = (long long)mid * (long long)mid; long long mid12 = (long long)(mid + 1) * (long long)(mid + 1); if (mid2 <= x && mid12 > x) return (int)mid; if (mid2 > x) r = mid -1; else l = mid +1; } }};
- leetcode Sqrt(x) Binary Search
- leetcode :Binary Search:Sqrt(x)(069)
- binary search---Sqrt(x)
- LeetCode 69 Sqrt(x)(Math、Binary Search)(*)
- 69. Sqrt(x)----binary search
- Binary Search:69. Sqrt(x)
- leetcode :Binary Search:Pow(x, n)(050)
- LeetCode: Sqrt(x)
- LeetCode sqrt(x)
- [Leetcode] Sqrt(x)
- LeetCode :Sqrt(x)
- leetcode 53: Sqrt(x)
- [LeetCode] Sqrt(x)
- Leetcode 69 sqrt(x)
- [Leetcode] Sqrt(x)
- leetcode:Sqrt(x)
- 【leetcode】sqrt(int x)
- [LeetCode]Sqrt(x)
- 【有趣的面试算法题】之五 各种整数集的处理
- InnoDB 存储引擎
- { }soj.1206
- 经典快速排序,linux纯C实现。注意swap方法,并且这个算法还需再敲写15篇。
- 网络第二课(3)---ip协议中的校验和。
- leetcode Sqrt(x) Binary Search
- WordNet
- 盐城市公交路线及时刻表
- 注册自己的二维码
- 新书推荐
- 一道面试题,考了我两次,两次都不会T_T
- 杭电ACM 1003 :Max Sum
- (简单递归3.2.1)POJ 1979 Red and Black(利用回溯法来扫图)
- IO多路复用