69. Sqrt(x)
来源:互联网 发布:办公软件基础教程 编辑:程序博客网 时间:2024/06/05 03:34
Implement int sqrt(int x)
.
Compute and return the square root of x.
可以用binary search,适合用upperbound的模板,模板:Binary Search - 二分搜索。因为num除了0、1外,sqrt的结果都会<=num/2,这样把start设成0,end设成n/2+2,条件设成mid>x/mid。注意这里不要用mid*mid>x,因为mid*mid会溢出。代码如下:public class Solution { public int mySqrt(int x) { int start = 0, end = x / 2 + 2; while (start + 1 < end) { int mid = start + (end - start) / 2; //don't use mid*mid > x, it will overflow if (mid > x / mid) { end = mid; } else { start = mid; } } return end - 1; }}
0 0
- LeetCode 69. Sqrt(x)
- LeetCode 69.Sqrt(x)
- LeetCode --- 69. Sqrt(x)
- [Leetcode] 69. Sqrt(x)
- [leetcode] 69.Sqrt(x)
- 69.Sqrt(x)
- 69. Sqrt(x)
- Leetcode-69.Sqrt(x)
- leetcode--69.sqrt(x)
- 69. Sqrt(x)
- LeetCode 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)
- 69. Sqrt(x) LeetCode
- 69. Sqrt(x)
- 69. Sqrt(x)
- [leetcode] 69. Sqrt(x)
- 69. Sqrt(x)
- 17/1/18/2
- php操作redis 封装的类
- Hadoop SSH配置无密码验证
- (c++)打印出所有"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该本身。 例如:153是一个水仙花数,因为153=1^3+5^3+3^3。
- 在VS中添加lib库的三种方法
- 69. Sqrt(x)
- 使用idea开发spark 实战
- 我的单片机世界第一盏灯
- 经济动向a
- 不依赖activity的全局对话框的实现
- nvidia-docker学习笔记
- 业务流程为主线的分解结构
- 单片机LED闪烁
- 使用java开发spark 实战