[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 ...
随机推荐
- php中memcached的使用
文档所在:http://php.net/manual/zh/book.memcached.php 1.在php使用memcached操作之前要实例化memcached: [系统类] 2.添加服务器: ...
- 在Ubuntu上安装Chrome Driver和Firefox Driver
在Ubuntu上安装Chrome Driver和Firefox Driver 此文章只介绍Chrome Driver(Firefox Driver和该步骤相同) 下载链接:http://chromed ...
- codeforces#525 Div2---ABC
A---Ehab and another constriction problem https://codeforc.es/contest/1088/problem/A 题意:给定一个数$x$找两个在 ...
- poj3376 Finding Palindromes【exKMP】【Trie】
Finding Palindromes Time Limit: 10000MS Memory Limit: 262144K Total Submissions:4710 Accepted: 8 ...
- <转>记dynamic的一个小坑 -- RuntimeBinderException:“object”未包含“xxx”的定义
→转载地址← 创建一个控制台程序和一个类库, 在控制台创建一个匿名对象,然后再在类库中访问它,代码如下: namespace ConsoleApplication1 { class Program { ...
- 查看线程的进程id
import os from threading import Thread def f1(n): print(n) print('%d号线程的id是%s'%(n,os.getpid())) if _ ...
- 1.7Oob 继承关系中构造方法的使用
1:父类中最好要有一个空参数的构造方法,因为默认的构造方法在自定义了构造方法后就不存在了,需要显示的写出来. 若父类中没有空参数的构造方法,则子类必须有自定义的构造方法,且用super()调用父类的构 ...
- xCode 升级9.3之后巨卡
因为项目要适配iPhone8, iPhoneX 等.需要升级Xcode需要升级到9.3.但是 MAC系统是10.12的,需要升级到10.13. 系统升级完之后升级Xcode.之后Xcode 就各种卡. ...
- sql 一对多查询最近一条
感谢 http://bbs.csdn.net/topics/391048578?page=1 create table A ( [Id] [uniqueidentifier] NOT NULL, ) ...
- 数学用语中的 透明 transitive property
1.透明 https://en.wikipedia.org/wiki/Equivalence_relation In mathematics, an equivalence relation is a ...