题目:求平方根

难度:Easy

题目内容

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.

翻译

计算并返回x的平方根,其中x保证是一个非负整数。

由于退货类型是一个整数,所以小数部分被截断,并且只返回结果的整数部分。

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.

我的思路:一个正整数(除了0和1)的平方根一定是从1到x/2,之间的某一个数,所以就相当于查找算法,所以采用二分法即可

    需要注意的是,我们的寻找条件,

  如果mid * mid > x   right = mid - 1;

  否则:此时mid * mid <= x,还满足,(mid+1) * (mid+1)> x 此时mid即可返回

     否则:left = mid +1;

我的代码:

     public int mySqrt(int x) {
if (x <= 1)
return x;
int left = 1, 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;
}
}
}

我的复杂度:O(n)

编程过程中的问题

1、因为mid在判断中也是判断过了的,不像Search in Rotated Sorted Array那样是等循环结束,所以left和right的下一个值都不需要再包括mid

答案代码:和我的一样。

LeetCode第[69]题(Java):Sqrt(x)的更多相关文章

  1. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[11]题(Java):Container With Most Water 标签:Array

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

  6. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  7. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  8. LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

  9. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

随机推荐

  1. JavaScript 学习(1)--window对象

    JavaScript 学习--Window对象 window对象方法 1.1 创建新窗体 //窗体打开 var newWindow=Window.open("default.html&quo ...

  2. http://www.nirsoft.net/about_nirsoft_freeware.html

    http://www.nirsoft.net/about_nirsoft_freeware.html

  3. 2015-03-20——移动端UC浏览器、QQ浏览器自动全屏

    UC:<meta name="full-screen" content="yes"><meta name="browsermode& ...

  4. AHOI2019退役记

    $DAY\quad -1$: 连作业都不写了来刷题... 希望能长点$RP$吧... 反正也是抱着退役的心情来考试... 我要是到了周日还不出长门我就退游!!! $DAY\quad 0$: 早上一起来 ...

  5. Android ViewGroup onInterceptTouchEvent

    public boolean onInterceptTouchEvent (MotionEvent ev) Implement this method to intercept all touch s ...

  6. 安装MySQL版本为mysql-installer-community-5.7.17.msi

    双击MySQL安装包, 勾选复选框,点击下一步: 选择仅仅服务器模式,点击下一步: 直接点击执行: 执行完成,点击下一步: 确认配置,点击下一步: 输入用户名和密码,点击下一步: 默认选项,点击下一步 ...

  7. BCrypt 加密实现

    Bcrypt百度百科: bcrypt,是一个跨平台的文件加密工具.由它加密的文件可在所有支持的操作系统和处理器上进行转移.它的口令必须是8至56个字符,并将在内部被转化为448位的密钥. 除了对您的数 ...

  8. Hadoop的eclipse1.1.2插件的安装和配置

    我的集群使用的hadoop版本是hadoop-1.1.2.对应的eclipse版本也是:hadoop-eclipse-plugin-1.1.2_20131021200005 (1)在eclipse的d ...

  9. 安卓ios app自动化测试用例模板

    import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.android.AndroidElem ...

  10. Python(文件处理)

    二.基本操作 #r''------------------>> r:原生字符串,不判断符号的含义#文件处理 f=open(r’c:\a.txt’,’r’,encoding=’utf-8’) ...