69. Sqrt(x)
来源:互联网 发布:幼儿认字软件 编辑:程序博客网 时间:2024/05/19 08:01
Implement int sqrt(int x)
.
Compute and return the square root of x.
题意:求x的平方根。
直接看代码:
public class Solution { public int mySqrt(int x) { if(x == 0) return 0; if(x ==1 || x==2 || x==3) return 1; int left = 2; int right = x/2; while(true){ int mid = left + (right-left)/2; if(mid > x/mid){ right = mid-1; }else{ if(mid+1 > x/(mid+1)) return mid; left = mid+1; } } }}通过二分法,在2和x/2中寻找答案。
阅读全文
1 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)
- redis--集群
- 练习 2015-08-15 管道流 用于线程之间交互数据
- hadoop的基本简介及安装、配置、使用(一)
- C++ 类中特殊的成员变量(常变量、引用、静态)的初始化方法
- 四色猜想
- 69. Sqrt(x)
- 2018.8.15总结3-生日聚会
- IDEA集成Git版本控制工具—配置Git和GitHub
- 关于 Android 程序员最近的状况
- Linux驱动学习(-)
- TensorFlow函数:tf.nn.in_top_k()
- unity截取相机所看到的内容并保存图片
- VCG文档
- Hbase的表属性①