Skip to content

Commit 6b5cbb3

Browse files
authored
Merge pull request doocs#111 from zhanary/master
Upload two kind of methods to 0069 with python2
2 parents e8f47b0 + 8ae924d commit 6b5cbb3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-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+

solution/0069.sqrt-x/Solution2.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Newton's Method [python2] - 28ms
2+
3+
class Solution(object):
4+
def mySqrt(self, x):
5+
r = x
6+
while r * r > x:
7+
r = (r + x/r)/2
8+
9+
return r

0 commit comments

Comments
 (0)