We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7467948 commit 6b14113Copy full SHA for 6b14113
LeetCode/Easy/0374-guess-number-higher-or-lower/0374-guess-number-higher-or-lower.java
@@ -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