763.partitionlabels(代码片段)

ruruozhenhao ruruozhenhao     2023-02-21     144

关键词:

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:

  1. S will have length in range [1, 500].
  2. S will consist of lowercase letters (‘a‘ to ‘z‘) only.

 

Approach #1: C++.

class Solution 
public:
    vector<int> partitionLabels(string S) 
        int len = S.length();
        int curLen = 0;
        vector<int> ans;
        
        for (int i = 0; i < len; ++i) 
            curLen = helper(S, i);
            for (int j = i+1; j < curLen; ++j) 
                curLen = max(curLen, helper(S, j));
            
            
            ans.push_back(curLen-i+1);
            i = curLen;
        
        
        return ans;
    
    
    int helper(string S, int index) 
        int curLen = index;
        int len = S.length();
        int temp;
        while (index >= 0) 
            curLen = index;
            index = S.find(S[curLen], curLen+1);
        
        
        return curLen;
    
;

  

Analysis:

I use helper function to find the last index of current character. In the partitionLabels function we traversing the string and find the min interval.

 

*leetcode763.partitionlabels(代码片段)

https://leetcode.com/problems/partition-labels/description/constintSIZE=256;classSolutionpublic:booljudge(intcnt[],inthash[])//boolret=true;for(inti=0;i<SIZE;i++)if(hash[i]= 查看详情

763.partitionlabels(代码片段)

Astring S oflowercaselettersisgiven.Wewanttopartitionthisstringintoasmanypartsaspossiblesothateachletterappearsinatmostonepart,andreturnalistofintegersrepresentingthesizeoftheseparts. Example1:Input:S="ababcbacadefegdehijhklij"Output:[9,7,8]Explanation:Thepartitionis"ababcbaca","defeg... 查看详情

763.partitionlabels分区标签

Astring S oflowercaselettersisgiven.Wewanttopartitionthisstringintoasmanypartsaspossiblesothateachletterappearsinatmostonepart,andreturnalistofintegersrepresentingthesizeoftheseparts.Example 查看详情

leetcode763.partitionlabels划分字母区间(中等)

一个字符串S,将其尽可能多的分割为子字符串,条件是每种字符最多只能出现在一个子串中。上面的示例中,字符串S中有多个a,这些a必须只能在第一个子串中,字母e出现在第二个子串中。这道题难点就是如何找到字符串的断... 查看详情

markdowngit代码片段(代码片段)

查看详情

csharp代码片段(代码片段)

查看详情

javascript代码片段(代码片段)

查看详情

textvisualbasic代码片段(代码片段)

查看详情

sqloracle代码片段(代码片段)

查看详情

swift代码片段(代码片段)

查看详情

java代码片段【安卓】(代码片段)

查看详情

shbash的代码片段(代码片段)

查看详情

markdownphpexcelnotes和代码片段(代码片段)

查看详情

javaandroid的代码片段(代码片段)

查看详情

javascriptjs-常用代码片段(代码片段)

查看详情

常用代码片段(代码片段)

单例模式privatestaticHttpUtilinstance;publicstaticsynchronizedHttpUtilgetInstance()if(instance==null)instance=newHttpUtil();returninstance; 查看详情

常用代码片段(代码片段)

单例模式privatestaticHttpUtilinstance;publicstaticsynchronizedHttpUtilgetInstance()if(instance==null)instance=newHttpUtil();returninstance; 查看详情

text代码片段很有用(代码片段)

查看详情