Skip to content

Commit b6b5f8d

Browse files
authored
Upload solution to 0069 with python2 - 40ms
1 parent 88db147 commit b6b5f8d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

solution/0069.sqrt-x/Solution.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# binary search [python2] - 40ms
2+
3+
class Solution(object):
4+
def mySqrt(self, x):
5+
if x == 0:
6+
return 0
7+
8+
left = 0
9+
right = x
10+
while True:
11+
mid = left + (right-left)/2
12+
if (mid * mid > x):
13+
right = mid - 1
14+
else:
15+
if (mid+1) * (mid+1) > x:
16+
return mid
17+
left = mid + 1
18+

0 commit comments

Comments
 (0)