LC 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:
Swill have length in range[1, 500].Swill 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的更多相关文章
- 763. Partition Labels 相同字母出现在同一块中,且块数最多
[抄题]: A string S of lowercase letters is given. We want to partition this string into as many parts ...
- [LeetCode] 763. Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- 【LeetCode】763. Partition Labels 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
- 763. Partition Labels
我一开始看数据范围很小,没怎么想就直接暴力了. 暴力的思路是: 对于每一段S的前缀,这个前缀中的每一个字母都不应该在前缀的补集中出现,所以直接循环i:0 to S.length然后对于每一次循环,再循 ...
- Leetcode 763. Partition Labels
思路:动态规划.对于属于coins的coin,只要知道amount-coin至少需要多少个货币就能表示,那么amount需要的货币数目=amount-coin需要的货币数目+1:如果amount-co ...
- [LeetCode] Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [Swift]LeetCode763. 划分字母区间 | Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- LeetCode - Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- LC 416. Partition Equal Subset Sum
题目 Given a non-empty array containing only positive integers, find if the array can be partitioned i ...
随机推荐
- 在MDK 中忽略(suppress) 某一个警告
文章转载自:http://www.51hei.com/bbs/dpj-29515-1.html 有时候我们需要在MDK中忽略掉某一个具体的warnning,怎么做呢? 只需在Misc Control中 ...
- 8.Dropout
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist = in ...
- python学习笔记-(十三)线程、进程、多线程&多进程
为了方便大家理解下面的知识,可以先看一篇文章:http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html 线程 1.什么是线程? ...
- LeetCode 69 x 的平方根
链接:https://leetcode-cn.com/problems/sqrtx 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数, ...
- shell遍历多个文件夹并进行批量修改文件名
问题:将图片名中的ing_变为0. 当前目录下:$ ls pic,change_name.sh pic/ |__kk1/ |__img_001.jpg |__img_002.jpg |__vv2/ | ...
- PPM / PGM / PBM 图像文件格式[转]
下面将详细介绍ppm文件 ppm文件是一种图像文件,有其自己的文件格式.ppm文件由两个部分组成:第一个部分是三行ASCII码,这个部分决定了图像的存储格式以及图像的特征:第二个部分就是图像的数据部分 ...
- JZOJ 5988 珂学计树题 (Burnside引理)
什么神题a-没学过Burnside引理a学了也做不来系列-考场没怎么看这题,上最后十分钟打了样例就溜了-然后这题爆0了. Here CODE #include <cctype> #incl ...
- POJ-3080-Blue jeans(KMP, 暴力)
链接: https://vjudge.net/problem/POJ-3080#author=alexandleo 题意: 给你一些字符串,让你找出最长的公共子串. 思路: 暴力枚举第一个串的子串,挨 ...
- 2017"百度之星"程序设计大赛 - 初赛(A) [ hdu 6108 小C的倍数问题 ] [ hdu 6109 数据分割 ] [ hdu 6110 路径交 ] [ hdu 6112 今夕何夕 ] [ hdu 6113 度度熊的01世界 ]
这套题体验极差. PROBLEM 1001 - 小C的倍数问题 题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6108 (2017"百度之星 ...
- C# List分组
//分组 8个为一组 List<List<string>> ArrayList = sArray.Select((x, i) => new { Index = i, Va ...