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 ...
随机推荐
- 百度编辑神器ueditor在ajax或form提交内容时候异常
百度编辑神器ueditor在ajax或form提交内容时候异常,一:⑴web.config中<system.web> <httpRuntime requestValidationMo ...
- Win7自带的系统备份还原功能如何去使用?
很多用户都会反映Win7系统使用过程中会出现系统或应用程序方面的小故障,针对这些小问题,再选择进行电脑系统的重装就有些过于麻烦了. 其实Win7系统内带有系统备份和还原的功能,可以在电脑系统出现小问题 ...
- Raspberrypi 安装完MySQL之后登录不了(ERROR 1698 (28000))
1.问题原因: 出现这是错误是因为 mysql 默认的 root 用户使用了 UNIX auth_socket_plugin 的用户认证方式,我们有下面两种方式处理问题: 修改 root 用户认证方式 ...
- Binlog_master
二进制日志 记录导致数据改变或潜在导致数据改变的SQL语句 记录已提交的日志 不依赖于存储引擎类型 功能:通过"重放"日志文件中的事件来生成数据副本 注意:建议二进制日志和数据文件 ...
- IPC之util.c源码解读
// SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/util.c * Copyright (C) 1992 Krishna Balasubramani ...
- pytest的使用
一.python安装 1.windows(server): 双击python-3.6.7-amd64.exe执行安装流程,使用默认安装方式即可. 安装完成后查看是否安装成功: C:\Users\Adm ...
- goquery 解析不了noscript
今天在用goquery的时候 解析noscript标签的时候.发现一直获取不到里面的元素. google得到.需要去除noscript标签. s.Find("noscript"). ...
- socket 测试工具java
SocketTest.jar http://sockettest.sourceforge.net/
- 第二章 Vue快速入门--12 事件修饰符的介绍
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- u-boot下的DM驱动模型 阶梯状 (转)
U-boot 下DM驱动模型的相关笔记要注意的关键两点: DM驱动模型的一般流程bind->ofdata_to_platdata(可选)->probe 启动,bind操作时单独完成的 ...