二分法
int mid = left + (right - left) / 2 ;//取中 当只有两个元素时候,结果靠左
重点注意返回结果是letters[left] 还是 letters[right]
public static char nextGreatestLetter(char[] letters, char target) {
int left = 0;
int right = letters.length-1;
while(left <= right) {//当左右两个指针重合
int mid = left + (right - left) / 2 ;//取中
if(letters[mid] > target) {//如果中点大于目标点
right = mid -1;//将右指针指向中点右边
}else if (letters[mid] <= target) {
left = mid + 1;//将左指针指向中点左边
}
}
//因为有序,最后结果区左指针
return left < letters.length ? letters[left] : letters[0];
}