LeetCoe-69 x 的平方根

题目

实现 int sqrt(int x) 函数。

计算并返回 x 的平方根,其中 x 是非负整数。

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

示例 1:

输入: 4
输出: 2
示例 2:

输入: 8
输出: 2
说明: 8 的平方根是 2.82842…,
由于返回类型是整数,小数部分将被舍去。


解法一

用的是牛顿法,具体推导见
https://leetcode-cn.com/problems/sqrtx/solution/er-fen-cha-zhao-niu-dun-fa-python-dai-ma-by-liweiw/

class Solution {
public:
    int mySqrt(int x) {
        if (x == 0)
            return 0;
        double last = 0, now = 1;
        while (now != last) {
            last = now;
            now = (now + x / now) / 2;
        }
        return (int)now;
    }
};

解法二

二分枚举平方根,注意可能会溢出。

class Solution {
public:
    int mySqrt(int x) {
        //注:在中间过程计算平方的时候可能出现溢出,所以用long long。
        long long i=0;
        long long j=x/2+1;//对于一个非负数n,它的平方根不会大于(n/2+1)
        while(i<=j)
        {
            long long mid=(i+j)/2;
            long long res=mid*mid;
            if(res==x) return mid;
            else if(res<x) i=mid+1;
            else j=mid-1;
        }
        return j;
    }
};

你可能感兴趣的:(数据结构与算法)