贪心算法+双指针
class Solution {
public List
<Integer> partitionLabels(String S
) {
int[] last
= new int[26];
int length
= S
.length();
for (int i
= 0; i
< length
; i
++) {
last
[S
.charAt(i
) - 'a'] = i
;
}
int start
= 0, end
= 0;
List
<Integer> partitions
= new ArrayList<>();
for (int i
= 0; i
< length
; i
++) {
end
= Math
.max(end
,last
[S
.charAt(i
) - 'a']);
if (i
== end
) {
partitions
.add(end
- start
+ 1);
start
= end
+ 1;
}
}
return partitions
;
}
}
转载请注明原文地址: https://lol.8miu.com/read-27488.html