-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution763.java
More file actions
34 lines (31 loc) · 1.1 KB
/
Copy pathsolution763.java
File metadata and controls
34 lines (31 loc) · 1.1 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
import java.util.ArrayList;
import java.util.List;
public class solution763 {
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> partitions = 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;
}
}
partitions.add(lastIndex- firstIndex +1);//假如在第一个和最后一个中 里面每一个字母最后出现的位置都在里面 这个就是结果
firstIndex = lastIndex + 1;
}
return partitions;
}
private int char2Index(char c) {
return c-'a';
}
}