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. 学习--Spring IOC源码精读

    Spring核心IOC的源码分析(转载) 原文地址:https://javadoop.com/post/spring-ioc#toc11 转载地址:https://blog.csdn.net/nuom ...

  2. 如何使windows7的默认共享可以被访问[转载]

        因为UAC的存在, 如果使用windows 7 的默认共享,比如 \abcc$ ,会被提示 无权限错误. 为了方便在局域网共享文件,找到了这个方法. Open the registry edi ...

  3. 单调队列优化&&P1886 滑动窗口题解

    单调队列: 顾名思义,就是队列中元素是单调的(单增或者单减). 在某些问题中能够优化复杂度. 在dp问题中,有一个专题动态规划的单调队列优化,以后会更新(现在还是太菜了不会). 在你看到类似于滑动定长 ...

  4. 2019.09.24 Device doesn't support wireless sync

    这个是flutter报错的信息: Device doesn't support wireless sync. AMDeviceStartService(device, CFSTR("com. ...

  5. sql关联查询更新速度慢的问题

    原语句 update B b set b.fid = (select f.id from F f where f.bid = b.id) ; 可以考虑用 begin for f in (select ...

  6. angular8 集成swiper, 并将swiper封装成公共组件

    安装Swiper npm install swiper --save 或者 yarn add swiper --save 在angular.json文件添加swiper.js和swiper.css   ...

  7. js创建对象的几种方式(工厂模式、构造函数模式、原型模式)

    普通方法创建对象 var obj = { name:"猪八戒", sayname:function () { alert(this.name); } } var obj1 = { ...

  8. Java-收邮件

    import java.util.Properties; import javax.mail.Folder; import javax.mail.Message; import javax.mail. ...

  9. Java-Base64Fiend工具类

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; impo ...

  10. 【Winfrom-Panel】Panel隐藏与显示,自动隐藏菜单, Auto-Hide Menu

    设计界面:2个button,一个panel 当鼠标移动到btnHome时,显示menuPanel 当鼠标离开btnHome时,隐藏menuPanel 当btnPin箭头向左时,menuPanel为自动 ...