Skip to content

Commit 69ae9dc

Browse files
refactor 374
1 parent f082224 commit 69ae9dc

File tree

1 file changed

+8
-11
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+8
-11
lines changed

src/main/java/com/fishercoder/solutions/_374.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,28 @@ public static class Solution1 {
1212
public int guessNumber(int n) {
1313
int left = 1;
1414
int right = n;
15-
while (left + 1 < right) {
15+
while (left < right) {
1616
int mid = left + (right - left) / 2;
1717
int g = guess(mid);
1818
if (g == 0) {
1919
return mid;
2020
} else if (g > 0) {
21-
left = mid;
21+
left = mid + 1;
2222
} else {
23-
right = mid;
23+
right = mid - 1;
2424
}
2525
}
26-
if (guess(left) == 0) {
27-
return left;
28-
}
29-
return right;
26+
return guess(left) == 0 ? left : right;
3027
}
3128

3229
/**
33-
* This is a fake guess method that I wrote just to compile/test, I'll have to change it to
34-
* another number other than 6 based on the number to be found.
30+
* This is a fake guess method that I wrote just to make the compiler happy,
31+
* 7 is just a completely random value.
3532
*/
3633
private int guess(int num) {
37-
if (num > 6) {
34+
if (num > 7) {
3835
return -1;
39-
} else if (num < 6) {
36+
} else if (num < 7) {
4037
return 1;
4138
} else {
4239
return 0;

0 commit comments

Comments
 (0)