[LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
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. 04/15/2019 UPdate: 利用binary search,找last index that i * i <= x. Time: O(lg n) , Space: O(1) Code
class Solution:
def sqrt(self, x):
if x < 2: return x
l, r = 1, x # r * r > x for sure
while l + 1 < r:
mid = l + (r - l)//2
value = mid * mid
if value > x:
r = mid
elif value < x:
l = mid
else:
return mid
return l
class Solution:
def sqrt(self, num):
ans = num
while ans*ans > num:
ans = (ans + num//ans)//2
return ans
[LeetCode] 69. Sqrt(x)_Easy tag: Binary Search的更多相关文章
- [LeetCode] 278. First Bad Version_Easy tag: Binary Search
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- [LeetCode] 162. Find Peak Element_Medium tag: Binary Search
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- [LeetCode] 704. Binary Search_Easy tag: Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- [LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 35. Search Insert Position_Easy tag: Binary Search
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- day_5.21 py 高级编程
1.禁止模块之间的循环调用 2.浅拷贝 只拷贝引用!!\ 3. 深拷贝 只要里面有引用就继续拷贝 4.copy,copy() 5. '''2018-5-21 11:39:52就业班 py高级 ...
- 别致的语言GO(GO语言初涉)
最近由于各种原因(好吧,其实是犯懒)已经许久没有再写新的博文了!最近正好在学习一门新的语言,所以正好记录一下自己的学习成果!最近利用每天晚上下班回来后的几小时,学习了Google开发的Go语言,算是对 ...
- position 有五个值:static、relative、absolute、fixed、inherit。
position 有五个值:static.relative.absolute.fixed.inherit. static 是默认值.就是按正常的布局流从上到下从左到右布局,平常我们做网页时,没有指定 ...
- PHP链接MySQL,查询数据库内容,删除数据库内容。。。记住链接公式!!!
//扩展类叫MySQLi MySQL是数据库,MySQLi是扩展 Id地址本地网络服务器的地址localhost 如果想链接别人的输入他的服务器id地址. //root代表的是数据库名, //poss ...
- KMP 算法详解
之前模模糊糊的理解了KMP,结果由于并不是完全弄清楚而导致自己在一道题目上疯狂的T,似乎是next函数写的有问题,于是痛心疾首的回来写一篇报告,警示自己 对KMP来说,匹配串的next数组是重中之重, ...
- TIC Read Status此类网络活动提醒隐藏
这个方法会使得NSLog输出失效,printf正常工作 不推荐使用,应该是很多操作均被关闭,需要详细了解该参数意义 OS_ACTIVITY_MODE = disable
- [daily][centos][sudo] sudo 报错
有时候, 比如在CentOS 6上. sudo 会报如下错误: sudo: must be setuid root 这是因为, sudo 命令, 没有SUID, [root@T209 ~]# ll / ...
- [development][vim] vim显示空白字符
1. 作为一个严谨的程序员,你必须关心你敲下过的没一个字符.其中包括空白字符. 2. 有时候你需要review别人的代码,对于哪些肆意使用tab,space,enter的人.你怎么发现那些被他们留下的 ...
- Flink - InputGate
初始化 Task List<InputGateDeploymentDescriptor> consumedPartitions = tdd.getInputGates(); // Cons ...
- oracle优化:避免全表扫描
http://blog.csdn.net/onetree2010/article/details/6098259