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 ...
随机推荐
- Maven项目解决Remove '@override' annotation终极方案
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- webConfig的使用
<sessionState mode="Off|InProc|StateServer|SQLServer" cookieless="true|false" ...
- 环信及时通讯 Laravel 扩展包
laravel-easemob 环信及时通讯 laravel 包开发,用于环信用户.群.聊天室等功能 github 地址 安装 加载包 "link1st/laravel-easemob& ...
- CentOS7 一个网卡配置多个IP地址
1.给网卡p8p1新创建配置文件(复制原来的p8p1,修改IP地址即可) ifcfg-p8p1:0 vim /etc/sysconfig/network-scripts/ifcfg-p8p1:0 D ...
- 嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray
在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...
- Mysql事务特性
事务概念 事务可由一条sql或者一组sql组成.事务是访问并更新数据库中各种数据项的一个程序执行单元. 事务会把数据库从一种一致状态转换为另一种一致状态.在数据提交工作时,可以确保要么所有修改都已经保 ...
- cocos2dx基础篇(26) 单例模式
单例模式,说的通俗一点就是:创建某个类的全局唯一静态实例对象.也就是说从它创建开始,一直到整个游戏程序结束才会释放资源,期间一直保存的着数据. 单例类在大部分游戏中应该是必不可少的部分,如整个游戏音乐 ...
- P2085 最小函数值
题目链接hhh:https://www.luogu.org/problemnew/show/P2085好嘛,运气真好,刚A掉序列合并,正好碰到这题,可以说是序列合并的升级版了 那么简单说一下思路,首先 ...
- 为什么要malloc()?何时要malloc()?如何使用malloc()?
今日写程序,突然想到一个问题,为什么有时候不要malloc,为什么有时候要呢!好好查资料才了解到一些原理. 函数原型:void *malloc(unsigned int num_bytes); //分 ...
- C++中利用迭代器删除元素会发生什么?
转自:https://blog.csdn.net/yf_li123/article/details/75003425#comments (1)对于关联容器(如map,set,multimap,mu ...