69. Sqrt(x) (JAVA)
Implement int sqrt(int x).
Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since
the decimal part is truncated, 2 is returned.
- 用循环实现二分法
- 用除法防止溢出
class Solution {
public int mySqrt(int x) {
int left = 1;
int right = (x>>1) + 1;
int mid = 1;
while(left <= right){
mid = left + ((right-left) >> 1);
if(mid < x/mid){ //用除法防止溢出
left = mid+1;
}
else if(mid > x/mid){
right = mid-1;
}
else return mid;
}
return right;
}
}
69. Sqrt(x) (JAVA)的更多相关文章
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- 69. Sqrt(x) - LeetCode
Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...
- LeetCode算法题-Sqrt(Java实现)
这是悦乐书的第158次更新,第160篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第17题(顺位题号是69). 计算并返回x的平方根,其中x保证为非负整数. 由于返回类型 ...
- 69. Sqrt(x)
题目: Implement int sqrt(int x). Compute and return the square root of x. 链接: http://leetcode.com/pr ...
- LeetCode 69. Sqrt(x) (平方根)
Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...
- [LeetCode] 69. Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- Leetcode 69. Sqrt(x)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
- 【一天一道LeetCode】#69. Sqrt(x)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
随机推荐
- servlet与jsp的九大内置对象
- Ruby Programming学习笔记
#将ARGV[0]转换成正则表达式类型 pattern= Regexp.new(ARGV[0]) #Devise gem包 Devise是Ruby中使用最广泛的身份验证gem包之一.Devise为我们 ...
- java.sql.SQLSyntaxErrorException: ORA-01795: 列表中的最大表达式数为 1000
后台报了一些异常日志,查阅后发现在 oracle 数据库中使用 in 关键字条件不能超过 1000 个,当时写查询语句时没有关注这个问题 总结一下解决方法 1.分多次查询,对查询要求不高的话.把入参的 ...
- DAY 2模拟赛
DAY2 依旧是yyx出题 依旧毒瘤 滞空(jump/1s/64M) 题目描述: pyy在平面上第一象限内发现了顺序n个平台(她可以踩在这些平台上,但必须从第i-1号平台跳跃至i-1号平台),这些平台 ...
- 请介绍下 adb、ddms、aapt 的作用
adb 是 Android Debug Bridge ,Android 调试桥的意思 ddms 是 Dalvik Debug Monitor Service,dalvik 调试监视服务. aapt 即 ...
- Promise.then链式调用
let a = new Promise((resolve,reject)=>{ resolve(1) }).then((r)=>{console.log(r)}).then(()=> ...
- 阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
循环打印 工厂了的打印先注释掉 打印出来了5次对象. 打印数字i同时,让i++操作.为了看他被常见了几次实例 调用保存的方法 没个都想都有一个唯一的实例.在创建对象的时候,重新初始化了i的值.所以i每 ...
- 禁止在DBGrid中按delete删除记录
procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin if (ssctr ...
- dapper使用时性能优化
数据库中类型 Area 数据库类型 varchar dapper 来操作数据库,不能直接写 sql Area=@Area) //dapper 对C#中的字符串类型 默认是对应数据库nva ...
- Web测试方法_01
一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时,使 ...