We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents d5137cd + 7cbfa03 commit 5ad0fe5Copy full SHA for 5ad0fe5
#374. Guess Number Higher or Lower.cpp
@@ -0,0 +1,25 @@
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
+ class Solution {
11
+ public:
12
+ int guessNumber(int n) {
13
+ int l=1,h=n,p;
14
+ while(1){
15
+ p=(h-l)/2+l;
16
+ if(guess(p)==1)
17
+ l=p+1;
18
+ else if(guess(p)==-1)
19
+ h=p-1;
20
+ else
21
+ break;
22
+ }
23
+ return p;
24
25
+ };
0 commit comments