Leetcode 69. Sqrt(x)

Implement int sqrt(int x).

思路: Binary Search  

 class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
l = 0
r = x while l <= r:
mid = (l+r)//2
if mid*mid < x:
l = mid + 1
elif mid*mid > x:
r = mid - 1
else:
return mid
return r
上一篇:LintCode题解之判断是否为平方数之和


下一篇:python中的list的方法