Skip to content

Latest commit

 

History

History
33 lines (23 loc) · 622 Bytes

69.md

File metadata and controls

33 lines (23 loc) · 622 Bytes

Sqrt(x)

Description

link


Solution

根据牛顿迭代法的公式不断逼近多次方程式的根,其实和神经网络中的梯度下降是一个意思,思路非常相似


Code

O(m * m)

class Solution:
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        r = x
        while r * r > x:
            r = (r + x / r) // 2
        return int(r)