• 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;
        }
    };
    
  • 相关阅读:
    最长公共前缀
    无重复字符的最长子串
    文章采集代码
    网络验证常见的攻击方式与防御手段
    初创公司如何避免服务器被攻击
    拒绝ssh远程暴力破解
    我公司开了7年,靠的就是这套顶级销售打法撑下来!
    顶级销售的十个习惯,轻松签下百万千万合同!(值得背下来)
    顶级销售高手总结的 9 个方面
    一位顶级销售高手总结的“销售心得”!
  • 原文地址:https://www.cnblogs.com/pk28/p/8282672.html
Copyright © 2020-2023  润新知