0374.cpp (392B)
1 class Solution { 2 public: 3 int guessNumber(int n) { 4 int low = 1, high = n; 5 while (true) { 6 int mid = low + (high - low) / 2; 7 switch (guess(mid)) { 8 case 0: return mid; 9 case 1: low = mid + 1; break; 10 case -1: high = mid - 1; break; 11 default: return -1; 12 } 13 } 14 return -1; 15 } 16 };