We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents e8f47b0 + 8ae924d commit 6b5cbb3Copy full SHA for 6b5cbb3
solution/0069.sqrt-x/Solution.py
@@ -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
@@ -0,0 +1,9 @@
+# Newton's Method [python2] - 28ms
+ r = x
+ while r * r > x:
+ r = (r + x/r)/2
+ return r
0 commit comments