Runtime: 80 ms, faster than 44.48% of Python3 online submissions for Sqrt(x).
学习了一下,可以用牛顿法等方法求解
参考:[LeetCode] Sqrt(x) 求平方根
class Solution:
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
if x >=0:
return int(x**0.5)
牛顿法
class Solution:
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
if x < 4 and x >= 1:
return 1
y = int(x/2)
while(y**2>x):
y = (y**2 + x)/(2*y)
y = int(y)
return y