Skip to content

Commit e6972d3

Browse files
committed
2020-05-09
1 parent a513c8c commit e6972d3

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

0069.x的平方根/0069-x的平方根.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ def mySqrt(self, x):
44
:type x: int
55
:rtype: int
66
"""
7-
if not x :
8-
return 0
9-
if x < 4:
10-
return 1
11-
start, end = 2, x // 2
12-
while 1:
13-
i = (start + end) // 2
14-
if i ** 2 <= x and (i + 1) ** 2 >x:
15-
return i
16-
elif i ** 2 < x:
17-
start = i + 1
18-
elif i ** 2 > x:
19-
end = i - 1
20-
21-
7+
left, right = 1, x
8+
while left <= right:
9+
mid = (left + right) // 2
10+
s = mid ** 2
11+
if s == x:
12+
return mid
13+
elif s < x:
14+
left = mid + 1
15+
elif s > x:
16+
right = mid - 1
17+
return left - 1

0 commit comments

Comments
 (0)