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.

  

这道题给了我们一个字符串S,然我们将其尽可能多的分割为子字符串,条件是每种字符最多只能出现在一个子串中。比如题目汇总的例子,字符串S中有多个a,这些a必须只能在第一个子串中,再比如所有的字母e值出现在了第二个子串中。那么这道题的难点就是如何找到字符串的断点,即拆分成为子串的位置。我们仔细观察题目中的例子,可以发现一旦某个字母多次出现了,那么其最后一个出现位置必须要在当前子串中,字母a,e,和j,分别是三个子串中的结束字母。所以我们关注的是每个字母最后的出现位置,我们可以使用一个HashMap来建立字母和其最后出现位置之间的映射,那么对于题目中的例子来说,我们可以得到如下映射:

a -> 8
b -> 5
c -> 7
d -> 14
e -> 15
f -> 11
g -> 13
h -> 19
i -> 22
j -> 23
k -> 20
l -> 21 建立好映射之后,就需要开始遍历字符串S了,我们维护一个start变量,是当前子串的起始位置,还有一个last变量,是当前子串的结束位置,每当我们遍历到一个字母,我们需要在HashMap中提取出其最后一个位置,因为一旦当前子串包含了一个字母,其必须包含所有的相同字母,所以我们要不停的用当前字母的最后一个位置来更新last变量,只有当i和last相同了,即当i = 8时,当前子串包含了所有已出现过的字母的最后一个位置,即之后的字符串里不会有之前出现过的字母了,此时就应该是断开的位置,我们将长度9加入结果res中,同理类推,我们可以找出之后的断开的位置,
class Solution {
public List<Integer> partitionLabels(String S) {
if(S == null || S.length() == 0){
return new ArrayList<Integer>();
}
Map<Character, Integer> map =new HashMap<>();
for(int i=0; i < S.length(); i++){
map.put(S.charAt(i), i); }
int start = 0;
int end = 0;
List<Integer> list = new ArrayList<Integer>();
for(int i=0; i<S.length(); i++){
char c = S.charAt(i);
int last = map.get(c);
if(last > end){
end = last;
}
if(end == i){
list.add(end - start+1);
start = end+1;
end = end+1;
} }
return list; }
}

LeetCode - Partition Labels的更多相关文章

  1. [LeetCode] Partition Labels 分割标签

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

  2. LC 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 分割标签

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

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

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

  5. Leetcode 763. Partition Labels

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

  6. [LeetCode] Partition Equal Subset Sum 相同子集和分割

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

  7. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  8. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

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

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

随机推荐

  1. mac以及centos下安装Elasticsearch 以及权限管理插件

    Elasticsearch安装(提前系统需要安装java环境)mac安装 brew install elasticsearch centos安装 下载ElasticSearch安装包,https:// ...

  2. Linux Hadoop集群搭建第二步:--------SSH免密登陆

    内容和Linux 搭建 Hadoop集群--Jdk配置相关联 三台虚拟机的操作 Linux SSH免密登陆: 参考网址:http://www.cnblogs.com/999-/p/6884861.ht ...

  3. 十八. Python基础(18)常用模块

    十八. Python基础(18)常用模块 1 ● 常用模块及其用途 collections模块: 一些扩展的数据类型→Counter, deque, defaultdict, namedtuple, ...

  4. sas 变量类型转换

    data b2: set b1; newbl=put(oldbl,10.); run; 根据转换后的类型灵活填写

  5. C#正则表达式类Match和Group类的理解

    正则表达式可以看做一种有特定功能的小型编程语言,在一段文本中定位子字符串.利用正则表达式可以快速地分析大量的文本以找到特定的字符模式:提取.编辑.替换或删除文本子字符串:或将提取的字符串添加到集合.正 ...

  6. Paxos工程实践

    Overview 是不是感觉看了这篇 Paxos算法 感觉完全没看懂?2333我也是 之前Paxos算法在工程实现的过程中,会遇到非常多的问题. Chubby Google Chubby是一个大名鼎鼎 ...

  7. shell脚本实例-mysql多机部署

    今天我给大家分享shell 安装mysql 多机部署的实例,本次实验是基于各个主机的公钥已经配置好了,如果还不会推送公钥的同学,可以看看我以前写的文章,那里面有写推公钥的实例,mysql 多机部署一般 ...

  8. 解决 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L的问题

    <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</a ...

  9. 利用Hackrf One进行GPS定位欺骗制作超级跑马机

    0×00 驾校的困惑 现行规定要求每个学员都必须在驾校练习够规定的学时,才能参加考试,在每台教练车上都安装有计时计程终端,学员刷卡刷指纹后开始累计里程.但是目前中国的很多驾校,存在车少人多的情况,假设 ...

  10. os.path和sys.path的区别

    os.path是module,包含了各种处理长文件名(路径名)的函数. sys.path是由目录名构成的列表,python从中查找扩展模块(python源模块)编译模块,或者二进制扩展),启动pyth ...