Skip to content

Commit f0f604e

Browse files
authored
Update SqrtX.java
1 parent c677a7e commit f0f604e

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

Easy/SqrtX.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ public int mySqrt(int x) {
33
if (x < 2) {
44
return x;
55
}
6-
int start = 2;
7-
int end = x / 2;
8-
while (start <= end) {
9-
int mid = start + (end - start) / 2;
10-
long num = (long) mid * mid;
11-
if (num > x) {
12-
end = mid - 1;
13-
}
14-
else if (num < x) {
15-
start = mid + 1;
16-
}
17-
else {
6+
int left = 2;
7+
int right = x / 2;
8+
while (left <= right) {
9+
int mid = (left + right) / 2;
10+
long square = ((long) mid) * mid;
11+
if (square > x) {
12+
right = mid - 1;
13+
} else if (square < x) {
14+
left = mid + 1;
15+
} else {
1816
return mid;
1917
}
2018
}
21-
return end;
19+
return right;
2220
}
2321
}

0 commit comments

Comments
 (0)