zoukankan      html  css  js  c++  java
  • 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;
        }
    };
    
  • 相关阅读:
    洛谷P5661 公交换乘(二分)
    洛谷P4047 [JSOI2010]部落划分(最小生成树)
    洛谷P2872 [USACO07DEC]Building Roads S(最小生成树)
    卸载重装VirtualBox回滚报错
    POJ1151 Atlantis(扫描线+线段树+离散化)
    QT入门-信号槽拓展
    Vue模板语法与常用指令总结
    Vue 生命周期
    querySelector和getElementById方法的区别
    ES6 Class(类)的继承与常用方法
  • 原文地址:https://www.cnblogs.com/pk28/p/8282672.html
Copyright © 2011-2022 走看看