题目链接
贪心
class Solution {public List<Integer> partitionLabels(String s) {int[] count = new int[27];Arrays.fill(count,0);// 统计元素最后一次出现的位置for(int i = 0; i < s.length(); i++){count[s.charAt(i) - 'a'] = i;}List<Integer> res = new ArrayList<Integer>();int left = 0;int right = 0;for(int i = 0; i < s.length(); i++){// 找到字符出现的最远边界right = Math.max(right, count[s.charAt(i) - 'a']);if(i == right){// 将符合条件的字符串长度插入resres.add(right - left + 1);left = i + 1;}}return res;}
}