[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 ...
随机推荐
- day10 多媒体(文字 图片 音频 视频)
1计算机表示图形的几种方式 bmp:以高质量保存 用于计算机 jpg:以良好的质量保存 用于计算机或者网络 png:以高质量保存 图片大小的计算公式:图片 ...
- centos7 LANMP 安装
没有开头语. 操作系统:CentOs7.6 64位. Nginx:系统自带 nginx1.12.2包. Mysql:系统自带 MariaDB 5.6 ,更换为 Mysql5.6 PHP:系统php5. ...
- Jenkins Docker安装及Docker build step插件部署配置
生产部署环境:A:192.168.1.2 B:192.168.1.3 两台服务器系统均是Centos 7.3 , Docker版本都1.12.6 Jenkins安装操作步骤: 1.在A服务器上使用命 ...
- ztree 文件夹类型的 树状图
未套程序的源代码: 链接:http://pan.baidu.com/s/1nuHbxhf 密码:4aw2 已套程序的源代码: css样式: /*发布邮件 选择领导弹窗*/ .xuandao{ disp ...
- bochs
● 制作一个硬盘 ./bximage 步骤与制作软盘的相似,完成后将bochs软件提示的最后一句话,添加到自己的配置文件里: dd if=loader.bin of=~/Softwares/bochs ...
- TOP100summit:【分享实录-猫眼电影】业务纵横捭阖背后的技术拆分与融合
王洋:猫眼电影商品业务线技术负责人.技术专家.主导了猫眼商品供应链和交易体系从0到1的建设,并在猫眼与美团拆分.与点评电影业务融合过程中,从技术层面保障了商品业务的平稳切换,同时也是美团点评<领 ...
- 运行里用\\加IP地址访问远程主机和用mstsc登录远程主机有什么区别??
\\ip是访问共享:mstsc是直接远端控制主机 "\\加IP",只要是windows,除win9x外,默认系统自带共享服务,除非有防火墙,或者手工关闭lanmanserver & ...
- POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对
http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...
- Fmod使用总结
1.查询相关文档的地址 http://www.fmod.org/forum/viewtopic.php?f=7&t=15762
- El表达式对照表
设置 session.getAttribute("date" "date") 取得username的值 (String)session.getValue( ...