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 c677a7e commit f0f604eCopy full SHA for f0f604e
Easy/SqrtX.java
@@ -3,21 +3,19 @@ public int mySqrt(int x) {
3
if (x < 2) {
4
return x;
5
}
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 {
+ int left = 2;
+ int right = x / 2;
+ while (left <= right) {
+ int mid = (left + right) / 2;
+ long square = ((long) mid) * mid;
+ if (square > x) {
+ right = mid - 1;
+ } else if (square < x) {
+ left = mid + 1;
+ } else {
18
return mid;
19
20
21
- return end;
+ return right;
22
23
0 commit comments