LeetCode-69-Sqrt(x)(二分法)-Medium
来源:互联网 发布:旋转轮胎修改数据损伤 编辑:程序博客网 时间:2024/05/21 09:08
题意理解:
实现sqrt()函数;
题目分析:
1. 典型的二分法;
2. 注意使用long保留中间值,避免整数相乘出现溢出;
解题代码:
public class Solution { private int process(int left, int right, int val){ //System.out.println("left= "+left+" right= "+right); if(right-left==1){ return left; } int half=left+(right-left)/2; long tmp=half; tmp*=tmp; //System.out.println("tmp= "+tmp); if(tmp==val){ return half; }if(tmp>val){ return process(left, half, val); }else{ return process(half, right, val); } } public int mySqrt(int x) { if(x==1 || x==0){ return x; } return process(1, x, x); }}
0 0
- LeetCode-69-Sqrt(x)(二分法)-Medium
- [Leetcode 69, Medium] Sqrt(x)
- 【LeetCode】(69)Sqrt(x)(Medium)
- Medium 69题 Sqrt(x)
- Leetcode 69. Sqrt(x) (Medium) (cpp)
- LeetCode-50-Pow(x, n)( 二分法)-Medium
- 【Medium】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)
- 【Codeforces Round 262 (Div 2)D】【构造】Little Victor and Set 集合最多取k数使得异或值尽可能小
- 【Codeforces Round 262 (Div 2)E】【爆搜 估价函数剪枝 搜索步长剪枝】Roland and Rose 整格放点 点对欧几里得距离平方和尽可能大
- Activity的最佳实践
- JQuery对HTML进行操作
- 一点一滴分析LinkIt Smart 7688 问题汇总
- LeetCode-69-Sqrt(x)(二分法)-Medium
- 第六届蓝桥杯省赛(c++)B组 星系炸弹
- xml 转对象
- The serializable class MyException does not declare a static final serialVersionUID field of type lo
- windows如何安装scrapy
- 【April Fools Day Contest 2014A】【愚人节脑洞 猜题意】The Great Game 剪刀石头布
- 【April Fools Day Contest 2014B】【愚人节脑洞 学会看编译信息】Mysterious Language 猜编译语言
- intellij idea创建运行spring boot项目时报错
- hdu2087 剪花布条--KMP