Implement int sqrt(int x).
Compute and return the square root of x.
实现sqrt(int x)函数。
class Solution:
# @param x, an integer
# @return an integer
def sqrt(self, x):
if x == 0 :
return x
if x == 1 or x == 2 or x == 3:
return 1
begin = 0
end = x
while begin < (end-1):
value = (begin + end) / 2
if value * value < x:
begin = value
elif value * value > x:
end = value
else:
begin = value
break
return begin