题目链接
思路:每次更新当前段的最后一个位置,直到当前搜索的位置就是当前段的最后的位置,那么就结束当前段,统计一个答案。
代码:
class Solution {
public List partitionLabels(String s) {
int count = 0;//记录个数
int last = -1;//记录目前段出现的字母的最后一个位置
int tem = -1;//记录当前字母出现的最后的位置
List list = new ArrayList<>();
int[] arr = new int[26];//记录位置
//统计每个字母出现的最后的位置
for(int i = 0; i< s.length();i++){
arr[s.charAt(i) - 'a'] = i;
}
for(int i = 0; i< s.length();i++){
count++;
tem = arr[s.charAt(i)-'a'];
last = last < tem ? tem : last;
if(i == last){//只有搜索到当前段的最后一个字母的时候,这个 段才统计完毕
list.add(count);
count = 0;
last = -1;
tem = -1;
continue;
}
}
return list;
}
}



