leetcode 69 implement the sqrt(x)
来源:互联网 发布:linux查看电源 编辑:程序博客网 时间:2024/05/29 21:30
Problem: Implement int sqrt(int x)
.
Compute and return the square root of x.
Solution: 二分法思想,要特别注意细节部分,有可能的溢出。
class Solution {public: int mySqrt(int x) { if(x < 0) return -1; if(x == 0) return 0; if(x == 1) return 1; long long left = 0; long long right = x /2 ; while(left <= right){ long long int mid = left + (right - left)/2; long long int val = mid*mid; if(val == x) return mid; else if(val < x) {left = mid + 1;} else {right = mid -1;} } long long tmp = left*left; if(tmp <= x) return left; else return right; }};
阅读全文
0 0
- leetcode 69 implement the sqrt(x)
- LeetCode-- Implement int sqrt(int x)
- The Solution to 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-69 Sqrt(x)
- Sqrt(x) - LeetCode 69
- leetcode 69:Sqrt(x)
- LeetCode---(69)Sqrt(x)
- Leetcode #69 Sqrt(x)
- LeetCode(69) Sqrt(x)
- leetcode 69: Sqrt(x)
- MVP架构模式入门(转)
- lsmod命令
- Android-记账本(三)-MainActivity
- 大神推荐书单
- Git进阶【一】
- leetcode 69 implement the sqrt(x)
- 我的csdn博客开通了
- 51采集PCF8591数据通过ESP8266上传C#上位机
- Android-记账本(四)-适配器和CostBean类
- Dubbo入门学习--动态代码编译器Compiler
- MySQL学习篇一校对规则
- 一起艳学Centos7(二)
- Node.js模块的概念
- ajax跟跨域