[leetcode]763. Partition Labels

[leetcode]763. Partition Labels


Analysis

周四不知道快不快乐—— [有点想回家过暑假的冲动!]

A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.
参考了大神的做法:https://blog.csdn.net/fuxuemingzhu/article/details/79265829

Implement

class Solution {
public:
    vector<int> partitionLabels(string S) {
        int len = S.size();
        map<char, int> end_pos;
        for(int i=0; i<len; i++)
            end_pos[S[i]] = i;
        vector<int> res;
        int left = 0;
        int right = 0;
        for(int i=0; i<len; i++){
            right = max(right, end_pos[S[i]]);
            if(right == i){
                res.push_back(right-left+1);
                left = i+1;
            }

        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/81366445