-
Notifications
You must be signed in to change notification settings - Fork 43
/
sqrtx.py
156 lines (136 loc) · 3.64 KB
/
sqrtx.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Given a non-negative integer x, compute and return the square root of x.
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.
Example 1:
Input: x = 4
Output: 2
Example 2:
Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
Constraints:
0 <= x <= 231 - 1
"""
# V0
# IDEA : binary search
class Solution(object):
def mySqrt(self, x):
# edge case
if not x or x <= 0:
return 0
if x == 1:
return 1
l = 0
r = x-1
while r >= l:
mid = l + (r-l)//2
#print ("l = " + str(l) + " r = " + str(r) + " mid = " + str(mid))
sq = mid** 2
if sq == x:
return mid
elif sq < x:
if (mid+1)**2 > x:
return mid
l = mid + 1
else:
r = mid - 1
# V0
# IDEA : binary search
class Solution(object):
def mySqrt(self, num):
if num <= 1:
return num
l = 0
r = num - 1
while r >= l:
mid = l + (r - l) // 2
if mid * mid == num:
return mid
elif mid * mid > num:
r = mid - 1
else:
l = mid + 1
return l if l * l < num else l - 1
# V0'
# IDEA : binary search
class Solution(object):
def mySqrt(self, x):
# NOTE : this approach calculates mid 2 times
low, high, mid = 0, x, int(x / 2)
while low <= high:
if mid * mid > x:
high = mid - 1
else:
low = mid + 1
# NOTE : this approach calculates mid 2 times
mid = int((low + high) / 2)
return mid
# V0''
# IDEA : binary search
class Solution(object):
def mySqrt(self, x):
l, r = 0, x
mid = (l + r) // 2
while r >= l:
if mid * mid == x: # optional
return mid
elif mid * mid > x:
### NOTE THE CONDITION
r = mid - 1
else:
### NOTE THE CONDITION
l = mid + 1
mid = (l + r) // 2
return mid
# V1
# http://bookshadow.com/weblog/2015/08/29/leetcode-sqrtx/
# https://blog.csdn.net/fuxuemingzhu/article/details/79254648
# IDEA : binary search
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
low, high, mid = 0, x, int(x / 2)
while low <= high:
if mid * mid > x:
high = mid - 1
else:
low = mid + 1
mid = int((low + high) / 2)
return mid
# V1'
# http://bookshadow.com/weblog/2015/08/29/leetcode-sqrtx/
# IDEA : Newton's method
# https://zh.wikipedia.org/wiki/%E7%89%9B%E9%A1%BF%E6%B3%95
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
t = x
while t * t > x:
t = int(t / 2.0 + x / (2.0 * t))
return t
# V2
# Time: O(logn)
# Space: O(1)
class Solution(object):
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
if x < 2:
return x
left, right = 1, x // 2
while left <= right:
mid = left + (right - left) // 2
if mid > x / mid:
right = mid - 1
else:
left = mid + 1
return left - 1