Skip to content

Instantly share code, notes, and snippets.

@luoheng23
Created December 27, 2019 02:37
Show Gist options
  • Save luoheng23/c5578eea97f58f6ab832ec19179a87d2 to your computer and use it in GitHub Desktop.
Save luoheng23/c5578eea97f58f6ab832ec19179a87d2 to your computer and use it in GitHub Desktop.
// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);
class Solution {
public:
int guessNumber(int n) {
int s = 1, e = n;
while (s < n) {
int g = ((n - s) >> 1) + s;
switch (guess(g)) {
case -1:
n = g - 1;
break;
case 1:
s = g + 1;
break;
case 0:
return g;
}
}
return s;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment