We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 09de67b commit cc08304Copy full SHA for cc08304
easy/Guess Number Higher or Lower.java
@@ -0,0 +1,27 @@
1
+//374. Guess Number Higher or Lower
2
+/**
3
+ * Forward declaration of guess API.
4
+ * @param num your guess
5
+ * @return -1 if num is higher than the picked number
6
+ * 1 if num is lower than the picked number
7
+ * otherwise return 0
8
+ * int guess(int num);
9
+ */
10
+
11
+public class Solution extends GuessGame {
12
+ public int guessNumber(int n) {
13
+ int l = 1;
14
+ int r = n;
15
16
+ // Find the first guess num that >= target num
17
+ while (l < r) {
18
+ final int m = l + (r - l) / 2;
19
+ if (guess(m) <= 0) // -1, 0
20
+ r = m;
21
+ else
22
+ l = m + 1;
23
+ }
24
25
+ return l;
26
27
+}
0 commit comments