-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqrt(x).py
More file actions
28 lines (26 loc) · 753 Bytes
/
Sqrt(x).py
File metadata and controls
28 lines (26 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 069. Sqrt(x)
class Solution:
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
if x == 0:
return 0
if 1 <= x <= 3:
return 1
base = 1
while 4 * base * base < x: # 这样比较我们可以找到base,使得base平方 < x <= (2base)平方
base *= 2
if 4 * base * base == x:
return 2 * base
lo, hi = base, base * 2
while lo < hi - 1: # 如果lo = hi - 1,停止循环,直接返回lo
mid = (lo + hi) // 2
if mid * mid == x:
return mid
elif mid * mid > x:
hi = mid
else:
lo = mid
return lo