python实现leetcode之69. x 的平方根
2021-09-08 本文已影响0人
深圳都这么冷
解题思路
牛顿迭代
ans的平方大于等于x并且ans+1的平方小于x时,就是所要的结果
否则,就使用牛顿迭代,不断靠近目标
关于什么是牛顿迭代,建议参阅任何一本微积分教材
69. x 的平方根
代码
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
rtv = 1
while not (rtv**2 <= x < (rtv+1)**2):
rtv = (rtv + x/rtv)/2
return rtv
