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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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. ...

  4. [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 ...

  5. [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 ...

  6. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

  7. [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 ...

  8. [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. ...

  9. [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 ...

随机推荐

  1. nodejs抓取页面内容,并分析有无某些内容的js文件

    nodejs获取网页内容绑定data事件,获取到的数据会分几次相应,如果想全局内容匹配,需要等待请求结束,在end结束事件里把累积起来的全局数据进行操作! 举个例子,比如要在页面中找有没有www.ba ...

  2. 170829、mybatis使用oracle和mybatis中批量更新

    一.mybatis执行批量更新batch update 的方法(mysql数据库) 1.数据库连接必须配置:&allowMultiQueries=true(切记一定要加上这个属性,否则会有问题 ...

  3. python金融与量化分析----Jupyter Notebook使用

    Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言.在本文中,我们将介绍 Jupyter notebook 的主要特性,以 ...

  4. linux内核添加模块

    参考: http://blog.csdn.net/gaoguoxin2/article/details/50220665 动态添加模块不需要编译内核. LINUX的模块主要由6部分组成: 1.模块的加 ...

  5. ML.NET 0.8特性简介

    本周.NET生态圈内的更新源源不断,除了.NET Core 2.2,ASP.NET Core 2.2和Entity Framework Core 2.2之外,ML.NET 0.8也一并登上舞台. 新的 ...

  6. FileZilla:425 Can't open data connection for transfer of解决办法

    状况描述: 服务器端采用的Windows 2008系统,安装了FileZilla Server,客户端采用的FileZilla Client,客户端在连接服务器端的时候,可以正常通过验证,但是在列出目 ...

  7. CH 1402 - 后缀数组 - [字符串hash]

    题目链接:传送门 描述 后缀数组 (SA) 是一种重要的数据结构,通常使用倍增或者DC3算法实现,这超出了我们的讨论范围. 在本题中,我们希望使用快排.Hash与二分实现一个简单的 $O(n \log ...

  8. [No0000172]Android Studio设置HTTP代理(可用)

    android SDK下载:http://www.androiddevtools.cn . 禁止第一次启动 到AS安装目录,打开bin目录,编辑idea.properties, 在文件末尾添加: di ...

  9. day 0314函数的进阶

    1.空间:内置空间,全局空间,局部空间. 内置空间:空间存放python解释器,为我们提供了方便的名字:input,print,str,list,tuple 三个空间的加载顺序: 内置空间>&g ...

  10. knn/kmeans/kmeans++/Mini Batch K-means/Affinity Propagation/Mean Shift/层次聚类/DBSCAN 区别

    可以看出来除了KNN以外其他算法都是聚类算法 1.knn/kmeans/kmeans++区别 先给大家贴个简洁明了的图,好几个地方都看到过,我也不知道到底谁是原作者啦,如果侵权麻烦联系我咯~~~~ k ...