Leetcode 763. Partition Labels

版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/83546443

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Partition Labels

2. Solution

  • Version 1
class Solution {
public:
    vector<int> partitionLabels(string S) {
        unordered_map<char, int> m;
        vector<int> result;
        vector<int> indices(S.length());
        for(int i = 0; i < S.length(); i++) {
            m[S[i]] = i;
        }
        for(int i = 0; i < S.length(); i++) {
            indices[i] = m[S[i]];
        }
        int max = 0;
        int start = 0;
        for(int i = 0; i < S.length(); i++) {
            if(indices[i] > max) {
                max = indices[i];
            }
            if(i == max) {
                result.push_back(max - start + 1);
                start = i + 1;
                max = 0;
            }
        }
        return result;
    }
};
  • Version 2
class Solution {
public:
    vector<int> partitionLabels(string S) {
        unordered_map<char, int> m;
        vector<int> result;
        for(int i = 0; i < S.length(); i++) {
            m[S[i]] = i;
        }
        int max = 0;
        int start = 0;
        for(int i = 0; i < S.length(); i++) {
            if(m[S[i]] > max) {
                max = m[S[i]];
            }
            if(i == max) {
                result.push_back(max - start + 1);
                start = i + 1;
                max = 0;
            }
        }
        return result;
    }
};

Reference

  1. https://leetcode.com/problems/partition-labels/description/
阅读更多

扫码向博主提问

SnailTyan

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 深度学习
  • PyTorch
  • OCR
  • Docker
  • Caffe
去开通我的Chat快问
换一批

Old Labels

04-09

Several thousand years ago, there was an ancient kingdom consisting of n cities numbered from 0 to n - 1. The city numbered 0 was the capital of the kingdom and there were n - 1 directed roads connecting the capital and other cities so that every other city could be reached from the capital.nnIn the National Library, there was a dusty book in a deep recess which recorded an amazing story. The story said that long long ago, for every road there had been a specified non-negative integer labeled onto it. What's more, for every city, we could find that all the roads starting from it had different labels. So with these labels, every city i could be expressed by the path with a series of numbers pi from the capital to it in order. But the book didn't say anything about how the roads were labeled and it seemed the way the roads were labeled was no longer known by anybody.nnOne day the king in the kingdom, Takamina, decided to relabel the roads so that the conditions in the story were satisfied. Also, a specified condition should also be met that the list of pi for cities without any road starting from it, namely pathi, was lexicographically minimized. To compare two lists, we first sort both lists separately and compare them as strings of paths (do as how strings compare while the elements of the strings are paths). To compare two paths, we also compare them as strings of numbers.nnSo you, the most talented programmer, can you solve the problem?nnInputnnThere are multiple test cases. The first line of the input is an integer T ≈ 100 indicating the number of test cases.nnFor each test case, the first line are two integers n and Q (1 ≤ n ≤ 1000, 0 ≤ Q ≤ n). Next n - 1 lines each containing two integers u and v indicating a road from u to v. Next Q lines each containing one integer c (1 ≤ c ≤ n) indicating a query to the c-th smallest path in pathi.nnOutputnnFor each query, if there are less than c cities without any road starting from it, print "Out of range." (without quotes), otherwise, print the c-th smallest path in one line with labels in the path separated by one space. Print a blank line after each case.nnSample Inputnn1n8 5n0 1n0 2n0 3n1 4n2 5n3 6n3 7n1n2n3n4n5nSample Outputnn0 0n0 1n1 0n2 0nOut of range.

没有更多推荐了,返回首页