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. day_5.25py

    作用域

  2. mysql命令大全用户管理相关命令

        1.登陆 mysql>mysql -uJDev -p 2.用户管理 mysql>use mysql; 3.查看有哪些登陆用户 mysql> select host,user, ...

  3. uni-app,wex5,APPcan,ApiCloud几款国内webapp开发框架的选型对比

    框架列表. https://www.cnblogs.com/xiaxiaxia/articles/5705557.html 前言 近期,要开一个新的项目,APP类型.最重要的需求就是能够随时调整APP ...

  4. poj3080 Blue Jeans【KMP】【暴力】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:21746   Accepted: 9653 Descri ...

  5. win10 Java环境变量,hadoop 环境变量

    妈呀,今天又重装了系统.需要装上java环境. 安装环境老百度,然后老掉坑.(path 路经) 1,新建环境变量 JAVA_HOME 2, 新建 CLASSPATH 环境变量 .;%JAVA_HOME ...

  6. [No0000159]C# 7 中的模范和实践

    关键点 遵循 .NET Framework 设计指南,时至今日,仍像十年前首次出版一样适用. API 设计至关重要,设计不当的API大大增加错误,同时降低可重用性. 始终保持"成功之道&qu ...

  7. HTML5:基本语句

    HTML5:超文本标记语言(HTML)第五次重大修改 我用的sulime编辑器,用Package contorl后安装emmet插件来编辑HTML代码,以下为sublime操作小技巧 ctrl+s 保 ...

  8. php之变量和常量

    PHP中的变量用一个美元符号后面跟变量来表示.变量名是区分大小写的. 变量与PHP中其它的标签一样遵循相同的规则.一个有效的变量名有字母或者下划线开头,后面跟上任意数量的字母,数字,或者下划线. 按照 ...

  9. [daily][netctl] netctl有线网络连接使用802.1x进行验证上网

    由于企业安全管理要求,需要验证上网.验证方式是账号密码+802.1x 目前先调研了一下方案,还没有实施,大概调研结果如下: 先参考:https://jlk.fjfi.cvut.cz/arch/manp ...

  10. fastreport好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了

    好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了 可以用FASTREPORT中的分组打印,你看一下里面的DEMO,里面都有的, 高版本的有suppressRepeat ...