File tree 1 file changed +8
-11
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +8
-11
lines changed Original file line number Diff line number Diff line change @@ -12,31 +12,28 @@ public static class Solution1 {
12
12
public int guessNumber (int n ) {
13
13
int left = 1 ;
14
14
int right = n ;
15
- while (left + 1 < right ) {
15
+ while (left < right ) {
16
16
int mid = left + (right - left ) / 2 ;
17
17
int g = guess (mid );
18
18
if (g == 0 ) {
19
19
return mid ;
20
20
} else if (g > 0 ) {
21
- left = mid ;
21
+ left = mid + 1 ;
22
22
} else {
23
- right = mid ;
23
+ right = mid - 1 ;
24
24
}
25
25
}
26
- if (guess (left ) == 0 ) {
27
- return left ;
28
- }
29
- return right ;
26
+ return guess (left ) == 0 ? left : right ;
30
27
}
31
28
32
29
/**
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 .
35
32
*/
36
33
private int guess (int num ) {
37
- if (num > 6 ) {
34
+ if (num > 7 ) {
38
35
return -1 ;
39
- } else if (num < 6 ) {
36
+ } else if (num < 7 ) {
40
37
return 1 ;
41
38
} else {
42
39
return 0 ;
You can’t perform that action at this time.
0 commit comments