题目
Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target.
Letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'.
答案
class Solution {
public char nextGreatestLetter(char[] letters, char target) {
if(target >= letters[letters.length - 1]) return letters[0];
target++;
int l = 0, r = letters.length;
while(l < r) {
int m = (l + r) / 2;
if(letters[m] == target) {
return target;
}
else if(letters[m] < target) {
l = m + 1;
}
else {
r = m;
}
}
return letters[r];
}
}