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:
S
will have length in range[1, 500]
.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的更多相关文章
- 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 ...
随机推荐
- 视频大文件分片上传(使用webuploader插件)
背景 公司做网盘系统,一直在调用图片服务器的接口上传图片,以前写的,以为简单改一改就可以用 最初要求 php 上传多种视频格式,支持大文件,并可以封面截图,时长统计 问题 1.上传到阿里云服务器,13 ...
- 2.Java NIO 简介
概述 Java NIO 是 JDK 1.4 发布的一套全新的IO API(New IO 简称 NIO),由于 JDK 1.7 对 NIO 的更新,目前 NIO 被广泛应用,以至于 将 JDK 1.7 ...
- 使用tomcat-maven-plugin自动化部署应用
目标:将应用自动打包并自动部署到远程tomcat容器. 1.在pom.xml中添加plugin <plugin> <groupId>org.apache.tomcat.mave ...
- 一个javascript面试题
javascript面试题代码: <script type="text/javascript"> function fun(x,y){ console.log(&quo ...
- 【ZJOI 2016】旅行者
题意 http://uoj.ac/problem/184 题解 大概是神题. 网格图上跑最短路有一个经典的优化方式:分治分组跑最短路. 对于这道题,设矩形长为 \(n\),宽为 \(m\),则对 \( ...
- libusb 3.0
1)usb3.0 在windows使用winusb时,出现ReadFile的input buffer 太大时,读不到数据问题? 2)linux下,usb 3.0 libusb_claim_interf ...
- linux内核 内存管理
以下内容汇总自网络. 在早期的计算机中,程序是直接运行在物理内存上的.换句话说,就是程序在运行的过程中访问的都是物理地址. 如果这个系统只运行一个程序,那么只要这个程序所需的内存不要超过该机器的物理内 ...
- TCP_Wrappers应用层防火墙
TCP_Wrappers是一个工作在应用层的安全工具,它只能针对某些具体的应用或者服务起到一定的防护作用.比如说ssh.telnet.FTP等服务的请求,都会先受到TCP_Wrappers的拦截. T ...
- MySQL(MariaDB)默认密码和修改方法
由于笔者只测试过Ubuntu 16.04.4.Ubuntu 19.04和Debian 9,此方法不确定在其他版本下适用. 本文章介绍的方法同样适用于这样的错误信息. ➜ ~ mysql -u root ...
- UNIX下socket通信 - UDP通信
一.UNIX下socket通信: socket套接字是一种可以进行网络通信的内核对象,它是一个唯一的标示符,一般称它为socket描述符. 注意:UDP通信需要客户端先发送消息,服务端先进行等待客户端 ...