-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path69_Sqrt(x).py
More file actions
36 lines (30 loc) · 758 Bytes
/
69_Sqrt(x).py
File metadata and controls
36 lines (30 loc) · 758 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
29
30
31
32
33
34
35
36
from typing import List
class Solution:
def mySqrt(self, x: 'int') -> 'int':
if x <= 1:
return x
l = 1
r = x
while l <= r:
m = l + (r - l) // 2
if m * m > x:
r = m - 1
else:
l = m + 1
return r
def main():
# Example:1
x = 4
print(Solution().mySqrt(x))
# Input: x = 4
# Output: 2
print('------------------------------')
# Example:2
x = 8
print(Solution().mySqrt(x))
# Input: x = 8
# Output: 2
# Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
print('------------------------------')
if __name__ == '__main__':
main()