一、前言
本题为LeetCode第763题,是一道 贪心算法 相关的算法题,难度中等。
本题链接:#763. 划分字母区间
二、题目
字符串S由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母只能出现在同一个片段中。返回一个表示每个字符串片段的长度的列表。
示例:
// 划分为"ababcbaca", "defegde", "hijhklij"
Input: S = "ababcbacadefegdehijhklij"
Output: [9, 7, 8]// 最多划分成3个片段
三、思路
这是一道求最优解的算法题,可以尝试使用贪心算法来解题。
本题中没有提供任何数组,只提供了一个字符串,可以将字符串中字符的位置、数量等信息统计成数组形式,以作为解题的数据。
将字符在字符串中最后一次出现的位置进行统计,得到每个字符c最后一次出现的位置end c;
初始化第一个片段的开始索引start = 0,结束索引end = 0;
对该字符串中的字符进行从左往右的遍历,对于每个字符c,若其最后一次出现的位置为end c ,则其片段的结束索引end一定大于等于end c,因此令end = max(end, end c);
当遍历到索引end后,当前片段结束,其长度为end - start + 1;
令start = end + 1,开始下一片段的访问;
重复上述步骤,直至遍历完字符。
四、Java代码
public class soultion {
public List partitionLabels(String S) {
// 统计字符在字符串中最后一次出现的位置
int[] positions = new int[26];
int len = S.length();
// S.charAt(i) - 97,'a'对应97,使得数组索引范围是[0,25],对应'a',...,'z'
for(int i = 0; i < len; i++) {
positions[S.charAt(i) - 97] = i;
}
List partitionLength = new ArrayList<>();
int start = 0;
int end = 0;
for(int i = 0; i < len; i++) {
end = Math.max(end, positions[S.charAt(i) - 97]);
if( i == end ) {
partitionLength.add( end - start + 1);
start = end + 1;
}
}
return partitionLength;
}
}
五、补充
/*
上面代码是官方解法,下面是我自己的解法,原理好懂但比较复杂
这解法原理和435题无重叠区间比较像
*/
public class Solution {
public List partitionLabels(String S) {
// 查询字符串中所有字符的起始和结束位置,97对应'a',122对应'z'
List positions = new ArrayList<>();
for(int i = 97; i <= 122; i++) {
if( S.indexOf(i) != -1 ) {
positions.add( new Integer[]{S.indexOf(i), S.lastIndexOf(i)} );
}
}
// 按照开始位置进行升序排序
Collections.sort(positions, Comparator.comparingInt(o -> o[0]));
int start = 0;
int end = positions.get(0)[1];
List list = new ArrayList<>();
for(int i = 1; i < positions.size(); i++) {
if( end > positions.get(i)[0]) {
end = Math.max(end, positions.get(i)[1]);
}else {
list.add( end -start + 1 );
start = end + 1;
end = positions.get(i)[1];
}
}
list.add(end - start + 1);
return list;
}
}