Skip to content

Commit 71cb435

Browse files
authored
374.Guess Number Higher or Lower
374.Guess Number Higher or Lower
1 parent afc98f9 commit 71cb435

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

guessNumber.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public:
2+
int guessNumber(int n) {
3+
int l =1;
4+
int r = n;
5+
while (l <=r){
6+
7+
int mid = l+(r-l)/2;
8+
int res = guess(mid);
9+
10+
if (res==0) return mid;
11+
else if (res == -1) {
12+
r = mid - 1;
13+
}
14+
else {
15+
l = mid + 1;
16+
}
17+
}
18+
return -1;
19+
}
20+
};

0 commit comments

Comments
 (0)