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 ...
随机推荐
- pre 布局
w
- 如果"一切是IO"“一切是file”是成立的,那么上述的想法也一定可以实现吧 awk对apache日志分析 ---
定时执行 自动化处理 直接入库 再去读取这个file入库: root@VM---ubuntu:/var/log/apache2# awk '{print $1 "\t" $7}' ...
- 利用python实现TCP和UDP服务器
利用python的socket模块可以实现基本的网络编程,并且只限于一对一的连接.当然,也可以在其基础上实现一个网络服务器,但由于太底层这种做法不被推荐.其实如果要实现一个网络服务器很简单,调用pyt ...
- Java 语言基础(一)
大多数编程语言都包括以下基本内容: 关键字 标识符 注释 常量和变量 运算符 语句 函数 数组 学习语言最重要的两点: 该语言基础的表现形式是什么 这些东西什么时候使用 关键字 在程序语言中有特殊含义 ...
- Smobiler实现扫描条码和拍照功能(开发日志八)
一.调用摄像头进行扫描 barcode000~2.jpg (198.62 KB, 下载次数: 5) 下载附件 2015-12-23 17:41 上传 具体步骤: 1. 加入TextBox控件:加入B ...
- 001-es6变量声明、解构赋值、解构赋值主要用途
一.基本语法 1.1.声明变量的六种方法 参看地址:http://es6.ruanyifeng.com/#docs/let let:局部变量,块级作用域,声明前使用报错 var:全局变量,声明前使用 ...
- zz Qt下 QString转char*和char []
以下内容为转载:http://www.cnblogs.com/Romi/archive/2012/03/12/2392478.html -------------------------------- ...
- CNN学习笔记:激活函数
CNN学习笔记:激活函数 激活函数 激活函数又称非线性映射,顾名思义,激活函数的引入是为了增加整个网络的表达能力(即非线性).若干线性操作层的堆叠仍然只能起到线性映射的作用,无法形成复杂的函数.常用的 ...
- python学习笔记:函数参数
1. 位置参数:一般的参数 2. 默认参数: def power(x, n=2): s = 1 while n > 0: n = n - 1 s = s * x return s 参数里有默认赋 ...
- 谁说程序员不懂爱情【Her】
2014.8.17日 我和我女朋友恋爱两周年的日子.走到今天很不容易,我很珍惜. 就想趁这机会,尽自己能力做一个特别的礼物. 这款应用两周前就开始计划了.也熬了几个晚上.接触wp开发时间不长,第一款应 ...