leetcode 69. Sqrt(x) 整数平方根(二分/牛顿迭代法)

文章目录

Easy

1369

1930

Add to List

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

C++

求平方根,可以用二分法,二分的写法有很多,比如可以这样写:

class Solution {
public:
    int mySqrt(int x) {
        if(!x)return x;
        int l=0,r=INT_MAX;
        while(l<r-1){
            int m=l+(r-l)/2;
            if(m>x/m) // m*m>x
                r=m;
            else // m*m<=x
                l=m;
        }
        return l;
    }
};

还可以这样写:

class Solution {
public:
    int mySqrt(int x) {
        if(!x)return x;
        int l=0,r=INT_MAX;
        while(l<=r){
            int m=l+(r-l)/2;
            if(m>x/m) // m*m>x
                r=m-1;
            else // m*m<=x
                l=m+1;
        }
        return l-1;
    }
};
class Solution {
public:
    int mySqrt(int x) {
        if(x<=1)return x;
        int l=0,r=x;
        while(l<=r){
            int m=l+(r-l)/2;
            if(m>x/m)r=m-1; // m*m>x
            else l=m+1;
        }
        return r;
    }
};

这样写:

class Solution {
public:
    int mySqrt(int x) {
        if(!x)return x;
        int l=0,r=INT_MAX;
        while(l<r){
            int m=l+(r-l)/2;
            if(m>x/m) // m*m>x
                r=m;
            else // m*m<=x
                l=m+1;
        }
        return l-1;
    }
};

还可以用牛顿迭代法,见 https://blog.csdn.net/m0_37795244/article/details/106002038#Exercise_Loops_and_Functions_500

在这里插入图片描述

class Solution {
public:
    int mySqrt(int x) {
        if(!x)return x;
        long long r=x;
        while(true){
            if(r*r<=x&&(r+1)*(r+1)>x)break;
            r=(r+x/r)/2; // newton iteration
        }
        return r;
    }
};

假如要扩展到浮点数的话:

class Solution {
public:
    double mySqrt(int x, double p) { // p 是精度要求
        if(!x)return x;
        double l=0,r=x;
        while(l<r){
            double m=l+(r-l)/2;
            if(fabs(m*m-x)<p)return m;
            if(m>x/m) // m*m>x
                r=m;
            else // m*m<=x
                l=m;
        }
        return l;
    }
};

Java

class Solution {
    public int mySqrt(int x) {
        if(x<=1)return x;
        int l=0,r=x;
        while(l<r){
            int m=l+(r-l)/2;
            if(m>x/m) //m*m>x
                r=m;
            else l=m+1;
        }
        return r-1;
    }
}
// newton
class Solution {
    public int mySqrt(int x) {
        if(x<=1)return x;
        long r=x;
        while(true){
            if(r*r<=x&&(r+1)*(r+1)>x)break;
            r=(r+x/r)/2;// r = r - (r*r-x)/(2*r)
        }
        return (int)r;
    }
}

Python

class Solution:
    def mySqrt(self, x: int) -> int:
        if x<=1:
            return x
        l,r=0,x
        while l<r:
            m=l+(r-l)//2
            if m>x//m: # m*m>x
                r=m
            else:
                l=m+1
        return r-1
# newton
class Solution:
    def mySqrt(self, x: int) -> int:
        if x<=1:
            return x
        r=x
        while True:
            if r*r<=x and (r+1)*(r+1)>x:
                break
            r=(r+x//r)//2
        return r

Go

func mySqrt(x int) int {
    if x<=1 {
        return x
    }
    l,r:=0,x
    for l<r {
        m:=l+(r-l)/2
        if m>x/m {
            r=m
        } else {
            l=m+1
        }
    }
    return r-1
}
// newton
func mySqrt(x int) int {
    if x<=1 {
        return x
    }
    y:=int64(x)
    r:=y
    for true {
        if r*r<=y && (r+1)*(r+1)>y {
            break
        }
        r=(r+y/r)/2
    }
    return int(r)
}
posted @ 2020-08-05 13:33  winechord  阅读(159)  评论(0编辑  收藏  举报