-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionLabels.java
More file actions
30 lines (27 loc) · 807 Bytes
/
PartitionLabels.java
File metadata and controls
30 lines (27 loc) · 807 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
package leetcode.string;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName PartitionLabels
* @Description 划分字母区间 https://leetcode-cn.com/problems/partition-labels/
* @Author changxuan
* @Date 2020/10/22 下午8:48
**/
public class PartitionLabels {
public List<Integer> partitionLabels(String S) {
int[] last = new int[26];
for (int i = 0; i < S.length(); i++) {
last[S.charAt(i-'a')] = i;
}
List<Integer> res = new ArrayList<>();
int start = 0, end = 0;
for (int i = 0; i < S.length(); i++) {
end = Math.max(end, last[S.charAt(i-'a')]);
if (end == i) {
res.add(end - start + 1);
start = end + 1;
}
}
return res;
}
}