LeetCode 69. Sqrt(x)

题目

Implement int sqrt(int x).

Compute and return the square root of x.

分析

实现整数平方根函数

代码

1
2
3
4
5
6
7
8
9
10
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
r = x
while r*r > x:
r = (r + x/r) / 2
return r

点评