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.

Runtime: 8 ms, faster than 43.40% of C++ online submissions for Partition Labels.

第一次实现用的是map,第二次实现用的是数组,差了一倍的运行速度。

#include <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <iostream>
using namespace std; class Solution {
public:
vector<int> partitionLabels(string S) {
int map[];
for(int i=; i<S.size(); i++){
map[(int)S[i]] = i;
}
int idx = ;
vector<int> ret;
while(idx < S.size()){
int tmpidx = idx;
int maxback = map[(int)S[tmpidx]]+;
while(tmpidx < S.size() && tmpidx < maxback){
maxback = max(maxback, map[(int)S[tmpidx]]+);
tmpidx++;
}
ret.push_back(maxback - idx);
idx = maxback;
}
return ret;
}
};

LC 763. Partition Labels的更多相关文章

  1. 763. Partition Labels 相同字母出现在同一块中,且块数最多

    [抄题]: A string S of lowercase letters is given. We want to partition this string into as many parts ...

  2. [LeetCode] 763. Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  3. 【LeetCode】763. Partition Labels 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...

  4. 763. Partition Labels

    我一开始看数据范围很小,没怎么想就直接暴力了. 暴力的思路是: 对于每一段S的前缀,这个前缀中的每一个字母都不应该在前缀的补集中出现,所以直接循环i:0 to S.length然后对于每一次循环,再循 ...

  5. Leetcode 763. Partition Labels

    思路:动态规划.对于属于coins的coin,只要知道amount-coin至少需要多少个货币就能表示,那么amount需要的货币数目=amount-coin需要的货币数目+1:如果amount-co ...

  6. [LeetCode] Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  7. [Swift]LeetCode763. 划分字母区间 | Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  8. LeetCode - Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  9. LC 416. Partition Equal Subset Sum

    题目 Given a non-empty array containing only positive integers, find if the array can be partitioned i ...

随机推荐

  1. fastadmin 隐藏操作栏按钮

    formatter: function (value, row, index) { var that = $.extend({}, this); $(table).data({"operat ...

  2. scroll js 原生

    1.当前位置滚动: document.documentElement.scrollTop 当前位置: 有可能是0 window.scrollTo(,document.documentElement.s ...

  3. deep_learning_CNN

    AI学习笔记——卷积神经网络(CNN) image.png 上篇文章简单地地介绍了神经网络和深度学习,在神经网络中,每一层的每个神经元都与下一层的每个神经元相连(如下图), 这种连接关系叫全连接(Fu ...

  4. Linux:fdisk

    fdisk [-l] 装置名称 选项与参数: -l:输入后面接的装置所有的partition内容.若仅有fdisk -l时,则系统将会把整个系统内能够搜寻到的装置的partition均列出来 fdis ...

  5. linux 计划任务超时控制和并发控制

    Linux crontab 的配置存放 /var/spool/cron 目录下. Linux crontab的日志文件存放再 /var/log/cron* timeout 命令 timeout 给脚本 ...

  6. TF启程

    我第一次开始接触到TensorFlow大概是去年五月份,大三下,如果一年多已过,我却还在写启程..这进度,实在汗颜.. 一个完整的tensorflow程序可以分为以下几部分: Inputs and P ...

  7. 51Nod 1831 PN表

    打出PN表来 发现合数除16,34,289都是赢 质数除2,17都是输 #include<bits/stdc++.h> using namespace std; bool prime(in ...

  8. cypress 3

    https://blog.csdn.net/u014647208/article/details/80525226 https://blog.csdn.net/kulala082/article/de ...

  9. aaa服务器

    验证 授权 记账 authentication\authorization.accounting

  10. python小练手题1

    1. """ Write a program which can compute the factorial of a given numbers. The result ...