LeetCode第[69]题(Java):Sqrt(x)
题目:求平方根
难度: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)的更多相关文章
- 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 + ...
- 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 ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- 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 ...
- LeetCode第[11]题(Java):Container With Most Water 标签:Array
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- 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 ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- 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 ...
随机推荐
- sklearn.svm包中的SVC(kernel=”linear“)和LinearSVC的区别
参考:https://stackoverflow.com/questions/45384185/what-is-the-difference-between-linearsvc-and-svckern ...
- 搜索过滤grep(win下为findstr)
搜索过滤grep(win下为findstr) 1.主要参数 [options]主要参数: -c:只输出匹配行的计数. -i:不区分大小写 -h:查询多文件时不显示文件名. -l:查询多文件时只输出包含 ...
- 【我的Android进阶之旅】解决错误:No enum constant com.android.build.gradle.OptionalCompilationStep.FULL_APK
今天在分支编译代码并允许之后,接着同步主干代码之后,再继续点击[Run]按钮允许程序的时候报错了,错误描述日志如下所示: 一.错误描述 Error:(1, 1) A problem occurred ...
- 【我的Android进阶之旅】解决bug:You need to use a Theme.AppCompat theme (or descendant) with this activity.
前言 今天用Android Studio 生成Activity的时候,默认继承AppCompatActivity ,而在AndroidManifest.xml我对该Activity设置了一个主题,然后 ...
- sys模块 logging模块 序列化模块
一 :sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 ...
- java生成jar包
Java编写的application程序是否可以终于形成一个类似于exe一样的可执行文件.难道就仅仅能用命令行执行? 通常有两种.一种是制作一个可运行的JAR文件包.然后就能够像.chm文档一样双击运 ...
- Python高级教程-sorted
Python中的排序算法 排序是程序中经常用到的算法.通常规定,对于两个元素x和y,如果认为x<y,则返回-1,如果认为x == y,则返回0,如果认为x > y,则返回1,这样,排序算法 ...
- WEB安全验收参考文档——From Github
文章https://xianzhi.aliyun.com/forum/read/793.html 里面涉及到了web安全验收参考文档: 其实github上老外对此也做过一些整理.详情参考:https: ...
- C++异常安全、copy and swap
异常安全的代码是指,满足两个条件 1异常中立性 : 是指当你的代码(包括你调用的代码)引发异常时,这个异常 能保持原样传递到外层调用代码.(异常中立,就是指任何底层的异常都会抛出到上层,也就相当于是异 ...
- window.event.keycode值大全
window.event.keycode值大全 event.keycode值大全 1 keycode 8 = BackSpace BackSpace 2 keycode 9 = Tab Tab 3 k ...