forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextGreatestLetter.java
More file actions
45 lines (41 loc) · 996 Bytes
/
NextGreatestLetter.java
File metadata and controls
45 lines (41 loc) · 996 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package normal;
/**
* @program JavaBooks
* @description: 744. 寻找比目标字母大的最小字母
* @author: mf
* @create: 2019/11/09 10:10
*/
/*
题目:https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target/
难度:easy
*/
/*
输入:
letters = ["c", "f", "j"]
target = "a"
输出: "c"
输入:
letters = ["c", "f", "j"]
target = "c"
输出: "f"
*/
public class NextGreatestLetter {
public static void main(String[] args) {
char[] letters = {'c', 'f', 'j'};
char target = 'a';
System.out.println(nextGreatestLetter(letters, target));
}
private static 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];
}
}