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

use Newton‘s method, the most important is ans = (ans + x/ans)/2

Code
class Solution:
    def sqrt(self, x):
        ans = x
        while ans * ans > x:
            ans = (ans + x//ans) //2
        return ans

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9478896.html

时间: 2024-10-29 13:33:08

[LeetCode] 69. Sqrt(x)_Easy tag: Binary Search的相关文章

[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., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise return -1.

leetcode - Lowest Common Ancestor of a Binary Search Tree

leetcode -  Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined

LeetCode --- 69. Sqrt(x)

题目链接:Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 这道题的要求是实现int sqrt(int x),即计算x的平方根. 考虑二分,即先令l和r分别为1和x/2+1(x的平方根一定小于等于x/2+1),然后m等于(l+r)/2,不断比较m*m和x的大小. 由于m*m的时候,可能溢出,因此可以用除法代替乘法,或者采用long long类型. 时间复杂度:O(logn) 空间复杂度:O(1)

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

leetcode -day19 Convert Sorted List to Binary Search Tree

1.  Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 分析:将一个升序排列的链表转换为平衡二叉搜索树,采用递归的方式,先找到链表的中点,作为二叉树的根,然后递归求解左右子树. 如下: class Solution { public: Tr

leetcode No109. Convert Sorted List to Binary Search Tree

Question: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 把有序链表转化成平衡的BST Algorithm: 把链表转化成数组,再根据leetcode No108. Convert Sorted Array to Binary Search Tree的方法 找到数组中间的元素,作为根节点,则根节点左边是左子树,根节点

【leetcode刷题笔记】Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 题解:需要找到二叉搜索树中乱序的两个节点,并把它们交换回来

[Leetcode][BST][Convert Sorted Array to Binary Search Tree]

把一个排好序的vector转换为一颗二分查找树. 很简单的题目,递归即可,保证边界不要出错. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 1

[LeetCode 题解]:Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题意:给定一个有序的链表,将其转换成平衡二叉搜索树 思路: 二分法 要构建一个平衡二叉树,二分法无疑是合适的,至于如何分是的代码简洁,就需要用到递归了. class Solution { public: // find middle element of the list Lis