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. 学习markdown语法

    12.内部跳转 [这是一个按钮](#测试标题) - 1 - 1 - 1 # 测试标题 这是一个按钮 1 1 1 测试标题 注:使用-代替空格

  2. 火狐浏览器报错“support.mozilla.org

    火狐浏览器有时候再打开新网页会报此错“support.mozilla.org 有时候火狐浏览器会出现如下状况 解决方法 在地址栏键入”about:config” 点击“我了解此风险” 在下方任意位置右 ...

  3. acm入门练习心得

    A题  两数相加等于n相乘等于m 问是否存在两数 写了超级久没有写出来,因为我的思路是 把m的因子都找出来,加加看,看等不等于n.后来发现m的因子还要考虑负数的情况,还要考虑m是不是负数的情况.太过麻 ...

  4. ubuntu16.04安装 catkin_tools

    参考:https://catkin-tools.readthedocs.io/en/latest/installing.html First you must have the ROS reposit ...

  5. CCPC-Wannafly Winter Camp Day4 Div1 - 咆咆咆哮 - [三分+贪心]

    题目链接:https://zhixincode.com/contest/18/problem/I?problem_id=267 题目描述 输入描述 输出描述 一行一个整数表示答案. 样例输入 1 32 ...

  6. Elasticsearch 2.3.3 JAVA api说明文档

    原文地址:https://www.blog-china.cn/template\documentHtml\1484101683485.html 翻译作者:@青山常在人不老 加入翻译:cdcnsuper ...

  7. [daily][samba] smbclient使用

    用的也不是太明白,反正凑合用吧. 在用之前,只得到了两个信息,1:ip 192.168.30.9.    2:可以免密登录. 1.  用这个命令看一看,主要是找到这个目录:Anonymous ┬─[t ...

  8. SQL instr()函数的格式

    格式一:instr( string1, string2 )    /   instr(源字符串, 目标字符串) 格式二:instr( string1, string2 [, start_positio ...

  9. java中获取字母和数字的组合

    package com.ccytsoft.wkc.util; import java.util.ArrayList; import java.util.List; import java.util.R ...

  10. python摸爬滚打之day8---文件操作

    1.文件读写的两种方式 1,  f = open("文件位置",mode = "r", encoding = "utf-8") conten ...