Leetcode 763. 划分字母区间 贪心
原题链接:Leetcode 763. 划分字母区间
class Solution {
public:vector<int> partitionLabels(string s) {int n=s.size();vector<int> res;int l=0,r=0;for(int i=0;i<n;i++){int j=n-1;while(j>r && s[j]!=s[i]){j--;}r=max(r,j);if(i==r) {res.push_back(r-l+1);l=i+1;r=l;}}return res;}
};
class Solution {
public:vector<int> partitionLabels(string s) {int n=s.size(),l=0,r=0;vector<int> res;vector<int> last_pos(26,0);for(int i=0;i<n;i++){last_pos[s[i]-'a']=i;}for(int i=0;i<n;i++){r=max(r,last_pos[s[i]-'a']);if(i==r) {res.push_back(r-l+1);l=i+1;r=l;}}return res;}
};