题目描述:给定一个有序的字符数组 letters 和一个字符 target,要求找出 letters 中大于 target 的最小字符,如果找不到就返回第 1 个字符。
示例:
输入:
letters = ["c", "f", "j"]
target = "a"
输出: "c"
输入:
letters = ["c", "f", "j"]
target = "c"
输出: "f"
输入:
letters = ["c", "f", "j"]
target = "d"
输出: "f"
输入:
letters = ["c", "f", "j"]
target = "g"
输出: "j"
输入:
letters = ["c", "f", "j"]
target = "j"
输出: "c"
输入:
letters = ["c", "f", "j"]
target = "k"
输出: "c"
public char nextGreatestLetter(char[] letters
, char target
) {
int n
= letters
.length
;
int l
= 0, h
= n
- 1;
while (l
<= h
) {
int m
= l
+ (h
- l
) / 2;
if (letters
[m
] <= target
) {
l
= m
+ 1;
} else {
h
= m
- 1;
}
}
return l
< n
? letters
[l
] : letters
[0];
}