Leetcode 简单17 x的平方根

x的平方根:

PHP 24ms:

牛顿迭代法,公式为(n + x/n)/2

class Solution {

    /**
     * @param Integer $x
     * @return Integer
     */
    function mySqrt($x) {
        $n = 1;
        while(!(pow($n,2) <= $x && pow($n+1,2) > $x)){
            $n = (int)(($n+$x/$n)/2);
        }
        return (int)$n;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36688622/article/details/87898865
今日推荐