leetcode 763. Partition Labels

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.

Example 1:
Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
Note:

S will have length in range [1, 500].
S will consist of lowercase letters ('a' to 'z') only.

思路:题目要求划分尽量多的区间,使得每个区间不能出现相同的字母。 那么,我们可以单独处理出每个字母(26个)在字符串中第一次出现的位置和最后一次出现的位置,然后对于这26个区间做一下合并。

class Solution {
public:
    vector<int> partitionLabels(string S) {
        vector< pair<int,int> > v(26);
        for (int i = 0; i < 26; ++i) {
            v[i].first = v[i].second = 1000;
        }
        for (int i = 0; i < S.size(); ++i) {
            int x = S[i] - 'a';
            v[x].second = i;
        }
        for (int i = S.size()-1; i >= 0; --i) {
            int x = S[i] - 'a';
            v[x].first = i;
        }
        sort(v.begin(), v.end());
        int vis[600] = {0};
        vector<int>ans;
        for (int i = 0; i < 26; ++i) {
            //cout << v[i].first << " " << v[i].second << endl;
            if (vis[i]) continue;
            int x = v[i].first;
            int y = v[i].second;
            if (x == 1000&& y == 1000)continue;
            for (int j = i + 1; j < 26; ++j) {
                int a = v[j].first;
                int b = v[j].second;
                if (a==1000)continue;
                if (a < y) {
                    y = max(y, b);
                    vis[j]=1;
                }
                else break;
            }
            ans.push_back(y-x+1);
            
        }
        return ans;
    }
};
posted on 2018-01-14 12:00  Beserious  阅读(211)  评论(0编辑  收藏  举报