-
Notifications
You must be signed in to change notification settings - Fork 1
/
69. Sqrt(x).py
49 lines (43 loc) · 1.18 KB
/
69. Sqrt(x).py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution:
"""
Runtime: 56 ms, faster than 13.05% of Python3 online submissions for Sqrt(x).
Memory Usage: 13.8 MB, less than 99.84% of Python3 online submissions for Sqrt(x).
"""
def mySqrt(self, x: int) -> int:
y = x //2
while y > 0:
if y ** 2 > x:
y = y // 2
elif y ** 2 == x:
return y
else:
break
while (y+1)**2 <= x:
z = 2
while (y + y // z) ** 2 > x:
z *= 2
y += max(y // z, 1)
return y
for i in range(0,10):
y = Solution().mySqrt(i)
print(y)
"""
Other Solution
Best memo - use. Good time.
Simply binary search on ints for best solution.
class Solution:
def mySqrt(self, x: int) -> int:
if x < 2:
return x
left, right = 2, x // 2
while left <= right:
guess = left + (right - left) // 2
sq = guess * guess
if sq > x:
right = guess - 1
elif sq < x:
left = guess + 1
else:
return guess
return right
"""