0744.cpp (542B)
1 class Solution { 2 public: 3 char nextGreatestLetter(vector<char> &letters, char target) { 4 if (target >= letters.back()) return letters.front(); 5 int low = 0, high = letters.size() - 1; 6 target++; 7 while (low < high) { 8 int mid = low + (high - low) / 2; 9 if (letters[mid] == target) 10 return letters[mid]; 11 else if (letters[mid] < target) 12 low = mid + 1; 13 else 14 high = mid; 15 } 16 return letters[high]; 17 } 18 };