解法:贪心 + 双指针
代码:
class Solution {
public:
vector<int> partitionLabels(string S) {
vector<int> out_;
int last_[26] = { 0 };
for(int i = 0; i < S.length(); i++)
last_[S[i] - 'a'] = i;
int start_ = 0, end_ = 0;
for(int j = 0; j < S.length(); j++) {
end_ = max(end_, last_[S[j] - 'a']);
if(j == end_) {
out_.push_back(end_ - start_ + 1);
start_ = j + 1;
}
}
return out_;
}
};