leetcode 69. Sqrt(x)

来源:互联网 发布:ubuntu qemu kvm 编辑:程序博客网 时间:2024/06/04 00:25

题目:求x的平方根

思路:牛顿法,https://en.wikipedia.org/wiki/Newton%27s_method

class Solution(object):
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x==0:return 0
        last=0.0;res=1.0
        while last!=res:
            last=res
            res=(res+x/res)/2
        return int(res)

0 0