Skip to content

Latest commit

 

History

History
24 lines (20 loc) · 453 Bytes

69.Sqrt(x).md

File metadata and controls

24 lines (20 loc) · 453 Bytes

方法一:牛顿迭代法

class Solution {
    int s;
    public int mySqrt(int x) {
        s = x;
        if (x == 0) return 0;
        return (int)sqrt(x);
    }

    public double sqrt(double x) {
        double res = (x + s / x) / 2;
        if (res == x) {
            return res;
        } else {
            return sqrt(res);
        }
    }
}