Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

Description

Implement int sqrt(int x).

Compute and return the square root of x.

Tags: Binary Search, Math

思路

题意是求平方根,参考牛顿迭代法求平方根,然后再参考维基百科的Integer square root即可。

class Solution {
    public int mySqrt(int x) {
        long n = x;
        while (n * n > x) {
            n = (n + x / n) >> 1;
        }
        return (int) n;
    }
}

结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:awesome-java-leetcode