We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e447e71 commit 9b37c0aCopy full SHA for 9b37c0a
Sqrt(x)69/README.md
@@ -0,0 +1,3 @@
1
+Implement `int sqrt(int x)`.
2
+
3
+Compute and return the square root of *x*.
Sqrt(x)69/Solution.java
@@ -0,0 +1,26 @@
+public class Solution {
+ public int mySqrt(int x) {
+ if (x < 0)
4
+ return -1;
5
+ else if (x == 0)
6
+ return 0;
7
8
+ long left = 1;
9
+ long right = x;
10
11
+ while (left < right) {
12
+ long mid = (left + right) >> 1;
13
+ long val = mid * mid;
14
15
+ if (val > x) {
16
+ right = mid;
17
+ } else if (val < x && mid > left) {
18
+ left = mid;
19
+ } else {
20
+ return (int)mid;
21
+ }
22
23
24
+ return (int)left;
25
26
+}
0 commit comments