Skip to content

Commit 6b14113

Browse files
committed
Time: 0 ms (100%), Space: 40.4 MB (64.28%) - LeetHub
1 parent 7467948 commit 6b14113

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Forward declaration of guess API.
3+
* @param num your guess
4+
* @return -1 if num is higher than the picked number
5+
* 1 if num is lower than the picked number
6+
* otherwise return 0
7+
* int guess(int num);
8+
*/
9+
10+
public class Solution extends GuessGame {
11+
public int guessNumber(int n) {
12+
int left = 1;
13+
int right = n;
14+
15+
if(n == 1) return 1;
16+
17+
while(left <= right){
18+
int mid = left + (right-left)/2;
19+
20+
if(guess(mid) == -1){ // down해야함
21+
right = mid - 1;
22+
}else if(guess(mid) == 1){ // up해야함
23+
left = mid + 1;
24+
}else{
25+
return mid;
26+
}
27+
}
28+
29+
return right;
30+
}
31+
}

0 commit comments

Comments
 (0)