题目:求平方根

难度: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. delphi -----(去掉窗口最大化,最小化、关闭),主窗口,和子窗口之间的设置

    一.去掉窗口最大化,最小化.关闭 borderIcons:biSystemMenu:false borderStyle:bsSizeable 二.主子窗口 主main: //调用子窗体procedur ...

  2. hdu 4512 吉哥系列故事——完美队形I【LCIS经典应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4512 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  3. Sublime text找不到.so文件

    在使用Sublime text打开一个android项目的时候,你会发现找不到.so文件. 解决方法: 点击Sublime text的Preferences,然后点击Settings,这时候出现设置的 ...

  4. 解决:function in namespace ‘std’ does not name a type + allocator_/nullptr/dellocator_ was not declared + base operand of ‘->’ has non-pointer type ‘std::vector<cv::Mat>’ 错误编译时报错(caffe)

    解决方法,用到了c++11,g++命令需要加上-std=c++11选项 附:g++默认的c++标准 gcc-6.4.0 gcc-7.2.0 默认是 -std=gnu++14gcc-4.3.6 gcc- ...

  5. [转载]Css设置table网格线(无重复)

    原文地址:Css设置table网格线(无重复)作者:依然贰零零柒 效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transition ...

  6. MySQL给字段唯一索引的三种方法

    建表时添加 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `stu_id` ) NOT NULL AUTO_INCREMENT, ` ...

  7. 链路的有效性检测 及 基于TCP的通信为什么需要RETRY

    一.链路的有效性检测 当网络发生单通.连接被防火墙Hang住.长时间GC或者通信线程发生非预期异常时,会导致链路不可用且不易被及时发现. 特别是异常发生在凌晨业务低谷期间,当早晨业务高峰期到来时,由于 ...

  8. BeanUtils.copyProperties()

    BeanUtils.copyProperties() PropertyUtils.copyProperties() 通过反射将一个对象的值赋值个另外一个对象(前提是对象中属性的名字相同). 后付前 P ...

  9. django联合查询

    假设A表的主键aid作为B表的外键,A表有属性name,那么想查询B表中name为abc的元素就可以这样写: B.objects.all().filter(aid__name = 'abc') __真 ...

  10. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders

    地址:http://codeforces.com/contest/765/problem/D 题目: D. Artsem and Saunders time limit per test 2 seco ...