12345678910111213141516171819202122 |
- /* The guess API is defined in the parent class GuessGame.
- @param num, your guess
- @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
- int guess(int num); */
- public class Solution extends GuessGame {
- public int guessNumber(int n) {
- int beg = 1, end = n;
- while (beg <= end) {
- int mid = beg + (end - beg) / 2;
- int val = guess(mid);
- if (val == -1) {
- end = mid - 1;
- } else if (val == 1) {
- beg = mid + 1;
- } else {
- return mid;
- }
- }
- return beg;
- }
- }
|