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)的更多相关文章

  1. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  2. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

  3. 69. Sqrt(x) - LeetCode

    Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...

  4. LeetCode算法题-Sqrt(Java实现)

    这是悦乐书的第158次更新,第160篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第17题(顺位题号是69). 计算并返回x的平方根,其中x保证为非负整数. 由于返回类型 ...

  5. 69. Sqrt(x)

    题目: Implement int sqrt(int x). Compute and return the square root of x. 链接:   http://leetcode.com/pr ...

  6. 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 ...

  7. [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 ...

  8. Leetcode 69. Sqrt(x)

    Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...

  9. 【一天一道LeetCode】#69. Sqrt(x)

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

随机推荐

  1. java链接Mysql出现警告:Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by

    Java使用mysql-jdbc连接MySQL出现如下警告: Establishing SSL connection without server's identity verification is ...

  2. vue项目使用cropperjs制作图片剪裁,压缩组件

    项目中裁剪图片效果 代码部分:(将上传部分 封装成一个组件直接调用当前组件) <template> <div id="demo"> <!-- 遮罩层 ...

  3. Kotlin学习入门笔记

    参考资料 官网:https://kotlinlang.org/ 官方文档:https://kotlinlang.org/docs/reference/ Kotlin 源码:https://github ...

  4. vue组件化初体验 全局组件和局部组件

    vue组件化初体验 全局组件和局部组件 vue组件化 全局组件 局部组件  关于vue入门案例请参阅 https://www.cnblogs.com/singledogpro/p/11938222.h ...

  5. setcookie()函数

    PHP PDO使用fetchAll()方法获取结果集 fetchAll()方法获取结果集中的所有行数据记录. fetchAll()方法的语法格式如下: array PDOStatement::fetc ...

  6. 使用tensorflow.data.Dataset构造batch数据集(具体用法在下一篇博客介绍)

    import tensorflow as tf import numpy as np def _parse_function(x): num_list = np.arange(10) return n ...

  7. kotlin之字符类型

    kotlin语言中,字符类型用Char表示,与java区别在于,kotlin中字符不能直接看成数字,如下: java中: void check (char c){ if(c==97){ } } kot ...

  8. 代码实现:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

    package com.loaderman.Coding; import java.util.Scanner; /*利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分 ...

  9. linux计划crontab

    linux计划crontab 启动crontab服务 一般启动服务用  /sbin/service crond start 若是根用户的cron服务可以用 sudo service crond sta ...

  10. CTF—攻防练习之ssh私钥泄露

    攻防练习1 ssh私钥泄露 靶场镜像:链接: https://pan.baidu.com/s/1xfKILyIzELi_ZgUw4aXT7w 提取码: 59g0 首先安装打开靶场机 没办法登录,也没法 ...