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;
        }
    };
    
  • 相关阅读:
    Minimum Depth of Binary Tree leetcode java
    Maximum Depth of Binary Tree leetcode java
    Symmetric Tree leetcode java
    Same Tree leetcode java
    Binary Tree Postorder Traversal leetcode java
    Binary Tree Preorder Traversal leetcode java
    Binary Tree Inorder Traversal leetcode java
    Combinations leetcode java
    一键清除Centos iptables 防火墙所有规则
    阿里云centos7.7x64安装open,并配置ip转发和nat伪装
  • 原文地址:https://www.cnblogs.com/pk28/p/8282672.html
Copyright © 2011-2022 走看看