763.partitionlabels(代码片段)

gsz- gsz-     2022-12-19     665

关键词:

题目描述:

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.

解题思路:

遍历S,找到S[i]在S中最后出现的位置j,此时遍历i到j中的每个元素S[index],若S[index]在S中最后出现的位置大于j时,j等于S[index]在S中最后出现的位置,当index等于j时结束内循环。

预先记录每个字母在S最后出现的位置可以提高运算时间。

代码:

 1 class Solution 
 2 public:
 3     vector<int> partitionLabels(string S) 
 4         vector<int> res;
 5         for (int i = 0; i < S.size();) 
 6             size_t j = S.find_last_of(S[i]);
 7             if (i == j) 
 8                 res.push_back(1);
 9                 i += 1;
10                 continue;
11             
12             for (int index = i + 1; index <= j; ++index) 
13                 size_t tmp = S.find_last_of(S[index]);
14                 if (tmp > j)
15                     j = tmp;
16             
17             res.push_back(j-i+1);
18             i = j + 1;
19         
20         return res;
21     
22 ;

 

*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代码片段很有用(代码片段)

查看详情