[Leetcode]Sqrt(x)
来源:互联网 发布:安卓改iphone6在线软件 编辑:程序博客网 时间:2024/04/30 08:36
Sqrt(x)
Total Accepted: 74812 Total Submissions: 310770 Difficulty: Medium
Implement int sqrt(int x).
Compute and return the square root of x.
Subscribe to see which companies asked this question
题目很简单,弄清题意。使用二分查找即可,有一点:输入不一定是个完全平方数,如果不是的话取根的下边界整数。
class Solution {public: int mySqrt(int x) { if(x <= 0) return 0; int l = 1,r = x; int mid; while(l <= r){ mid = l + ((r - l) / 2); if(mid > x / mid){ r = mid - 1; } else if(mid < x / mid){ l = mid + 1; } else{ return mid; } } return r; }};
0 0
- 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)
- [leetcode]Sqrt(x)
- LeetCode-Sqrt(x)
- [leetcode] Sqrt(x)
- [leetcode] Sqrt(x)
- LeetCode - Sqrt(x)
- leetcode之sqrt(x)
- LeetCode | Sqrt(x)
- HDOJ 5479 Scaena Felix (栈)
- Roman to Integer
- UIWindow和UIView
- Fedora系统初次安装与配置(兼容win10双系统)
- Makefile浅析(一)
- [Leetcode]Sqrt(x)
- 视觉识别领域会议列表
- POJ2299 Ultra-QuickSort 归并排序和逆序数,树状数组
- sharesdk分享qq空间-错误码1001
- c++primer第五版第十二章练习
- 用php写每天固定时间要执行的代码
- 【HDU 1005】Number Sequence
- ViewController
- view 中的 opaque , alpha , 和 alpha 的区别