[LeetCode]69. Sqrt(x)
来源:互联网 发布:怎样成为淘宝客挣钱 编辑:程序博客网 时间:2024/06/14 09:04
[LeetCode]69. Sqrt(x)
题目描述
思路
由于题目要求返回的是整数,简单实现的话可以使用二分
注意实现过程中的一些越界问题
代码
#include <iostream>using namespace std;class Solution {public: int mySqrt(int x) { if (x == 1) return 1; int start = 0, end = x; while (start < end) { //越界点1 end + start 会越界 int mid = ((end - start) >> 1) + start; //越界点2 mid * mid 会越界 if (mid == x / mid) return mid; else if (start == mid || end == mid) return start; else if (mid < x / mid) start = mid; else end = mid; } return end; }};int main() { Solution s; cout << s.mySqrt(2147395599) << endl; system("pause"); return 0;}
阅读全文
0 0
- 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)
- 69. Sqrt(x) LeetCode
- [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)
- JAVA基础知识
- mui 模拟自定义事件
- SpringMVC源码(四)Controller控制器2-AbstractController
- 进度条
- Android第三方开源库:PopupWindow相关
- [LeetCode]69. Sqrt(x)
- 单例模式
- csv.Error: iterator should return strings, not bytes
- QtWidget: 自定义不规则窗体与按钮
- Android NavigationBar(菜单栏,导航栏)问题记录
- Java基础入门(四)选择结构
- HDOJ 1003Max Sum (dp)
- 解决Mysql中只能通过localhost登陆不能通过ip登陆的问题
- fragment下找不到getWindowManager() getLayoutInflater()方法