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;
        }
    };
    
  • 相关阅读:
    爬虫基础1 怎么使用浏览器查看网络请求
    前端模块化总结(commonJs,AMD,CMD,ES6 Module)
    js强制类型转换规则
    vue-cli3中使用mxgraph的一些总结
    js概念笔记(this)
    js概念笔记(for循环,模块化)
    根据对象数组的某一属性排序
    从svg诞生的panda
    gulp笔记(从开发到发布)
    webpack4.x学习笔记
  • 原文地址:https://www.cnblogs.com/pk28/p/8282672.html
Copyright © 2011-2022 走看看