-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution744.java
More file actions
29 lines (26 loc) · 848 Bytes
/
Solution744.java
File metadata and controls
29 lines (26 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package algorithm.leetcode;
/**
* @author: mayuan
* @desc:
* @date: 2018/08/19
*/
public class Solution744 {
public static void main(String[] args) {
char[] array = {'c', 'f', 'j'};
Solution744 test = new Solution744();
System.out.println(test.nextGreatestLetter(array, 'd'));
}
public char nextGreatestLetter(char[] letters, char target) {
int left = 0, right = letters.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (letters[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// 找出 letters 中大于 target 的最小字符,如果找不到就返回第 1 个字符。
return (left < letters.length) ? letters[left] : letters[0];
}
}