Skip to content

Commit 5ad0fe5

Browse files
authored
Merge pull request #87 from InflixOP/patch1
Create #374. Guess Number Higher or Lower.cpp
2 parents d5137cd + 7cbfa03 commit 5ad0fe5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)