-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_763.java
More file actions
35 lines (32 loc) · 1.01 KB
/
a_763.java
File metadata and controls
35 lines (32 loc) · 1.01 KB
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
package greedy;
import java.util.ArrayList;
import java.util.List;
/**
* 划分字母区间
*/
public class a_763 {
public List<Integer> partitionLabels(String S) {
int lastIndexOfChar[] = new int[26];
//返回字符在字符串中最后一次出现的索引下标
for (int i = 0; i < S.length(); i++) {
lastIndexOfChar[char2Index(S.charAt(i))] = i;
}
List<Integer> partition = new ArrayList<>();
int firstIndex = 0;
while (firstIndex < S.length()) {
int lastIndex = firstIndex;
for (int i = firstIndex; i < S.length() && i <= lastIndex; i++) {
int index = lastIndexOfChar[char2Index(S.charAt(i))];
if (index > lastIndex) {
lastIndex = index;
}
}
partition.add(lastIndex - firstIndex + 1);
firstIndex = lastIndex + 1;
}
return partition;
}
private int char2Index(char c) {
return c - 'a';
}
}