Jan 19 - Sqrt(x); Math; Binary Search;

We should keep in mind that never let the integer value overranged or overstacked..

Here is the trick, instead of comparing the square of a interger with the target value, we can compare the integer value i with the quotient x/i;

if(i > x/i) high = i - 1; if(i < x/i) low = i +1, the condition of the while is loop is low < high. Thus when the loop ends, low = high. There are two results, one is the low point to the integer which is the sqrt(x), the other is low = sqrt(x)+1. We need to check the two cases in the end.

Code:

public class Solution {
    public int mySqrt(int x) {
        if(x<0) return -1;
        if(x == 0) return 0;
        if(x == 1) return 1;
        int low = 1, high = x;
        while(low < high){
            int mid = low+(high-low)/2;
            int quotient = x/mid;
            if(quotient == mid) return mid;
            else if(mid > quotient){
                high = mid-1;
            }
            else{
                low = mid + 1;
            }
        }
        int quotient = x/low;
        if(low > quotient) return low -1;
        return low;
    }
}

  

Newton‘s method to get square root:

Code:

public int mySqrt(int x) {
    if (x == 0) return 0;
    long i = x;
    while(i > x / i)
        i = (i + x / i) / 2;
    return (int)i;
}
时间: 2024-07-30 13:41:21

Jan 19 - Sqrt(x); Math; Binary Search;的相关文章

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

LeetCode 50 Pow(x, n)(Math、Binary Search)(*)

翻译 实现pow(x, n). 原文 Implement pow(x, n). 分析 首先给大家推荐维基百科: zh.wikipedia.org/wiki/二元搜尋樹 en.wikipedia.org/wiki/Binary_search_tree 其次,大家也可以看看类似的一道题: LeetCode 69 Sqrt(x)(Math.Binary Search)(*) 然而这题我还是没有解出来,看看别人的解法-- class Solution { private: double myPowHel

19.3.2 [LeetCode 99] Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

A1043. Is It a Binary Search Tree (25)

Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: 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 greater

A1064. Complete Binary Search Tree (30)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: 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 greate

Complete Binary Search Tree

本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师.何钦铭老师的<数据结构> Complete Binary Search Tree 1 Question A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than

1099. Build A Binary Search Tree (30)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: 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 greate

LeetCode Closest Binary Search Tree Value

原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Note: Given target value is a floating point. You are guaranteed

Lintcode: Search Range in Binary Search Tree

Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. Example