We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a513c8c commit e6972d3Copy full SHA for e6972d3
0069.x的平方根/0069-x的平方根.py
@@ -4,18 +4,14 @@ def mySqrt(self, x):
4
:type x: int
5
:rtype: int
6
"""
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
+ left, right = 1, x
+ while left <= right:
+ mid = (left + right) // 2
+ s = mid ** 2
+ if s == x:
+ return mid
+ elif s < x:
+ left = mid + 1
+ elif s > x:
+ right = mid - 1
+ return left - 1
0 commit comments