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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
又是index从0开始,长度不够因此需要加1
[思维问题]:
知道是前向指针,但是不知道怎么确定前面的i:存map[26],然后i顺着循环一遍就行了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
不知道last怎么时候保持最大:math.max也是记忆化搜索的一种方式
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
确定前面的i:存map[26],然后i顺着循环一遍就行了
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<Integer> partitionLabels(String S) {
//initialization: result, int[26]
List<Integer> result = new ArrayList<Integer>();
int[] map = new int[26];
//store the last position into the map
for (int i = 0; i < S.length(); i++) {
map[S.charAt(i) - 'a'] = i;
}
//for loop: get the last, add last - start, reset start
int start = 0;
int last = 0;
for (int i = 0; i < S.length(); i++) {
//get the last
last = Math.max(last, map[S.charAt(i) - 'a']);
//add last - start, reset start
if (i == last) {
result.add(last - start + 1);
start = last + 1;
}
}
//return
return result;
}
}
763. Partition Labels 相同字母出现在同一块中,且块数最多的更多相关文章
- 使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页
使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页 方法1:linux下使用awk命令 # cat access1.log | awk '{print $1" &q ...
- LC 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 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- 763. Partition Labels
我一开始看数据范围很小,没怎么想就直接暴力了. 暴力的思路是: 对于每一段S的前缀,这个前缀中的每一个字母都不应该在前缀的补集中出现,所以直接循环i:0 to S.length然后对于每一次循环,再循 ...
- 【LeetCode】763. Partition Labels 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
- Leetcode 763. Partition Labels
思路:动态规划.对于属于coins的coin,只要知道amount-coin至少需要多少个货币就能表示,那么amount需要的货币数目=amount-coin需要的货币数目+1:如果amount-co ...
- 【python cookbook】【数据结构与算法】12.找出序列中出现次数最多的元素
问题:找出一个元素序列中出现次数最多的元素是什么 解决方案:collections模块中的Counter类正是为此类问题所设计的.它的一个非常方便的most_common()方法直接告诉你答案. # ...
- python 找出字符串中出现次数最多的字母
# 请大家找出s=”aabbccddxxxxffff”中 出现次数最多的字母 # 第一种方法,字典方式: s="aabbccddxxxxffff" count ={} for i ...
- 找出此产品描述中包含N个关键字的长度最短的子串
阿里巴巴笔试题:给定一段产品的英文描述,包含M个英文字母,每个英文单词以空格分隔,无其他标点符号:再给定N个英文关键词,请说明思路并变成实现方法. String extractSummary(Stri ...
随机推荐
- PythonStudy——Python中的None与 NULL(即空字符)的区别
None与 NULL(即空字符)的区别 (1)是不同的一种数据类型 >>>type(None) <class 'NoneType'> >>>type( ...
- awk命令过滤tomcat的访日日志中IP地址
1. 命令如下 批量过滤日志文件,grep -v是要排除10网段开头的IP地址 sort会自动按ip排序 uniq -c去重并计数 sort -n 按数值从小到大排序 [root@linux-node ...
- 第2章 Java基本语法(上): 变量与运算符
2-1 关键字与保留字 关键字(keyword) 保留字(reserved word) 2-2 标识符(Identifier) 案例 class Test{ public static void ma ...
- ElasticSearch的lowlevelApi和低级别API
之前开发使用的其实都是lowLevel的api,所谓lowlevelapi就是操作ES的json字符串要自己去写:所谓highlevel的api就是指将查询的json字符串给对象化,创建一个Searc ...
- SVN 撤回已提交的代码
1. TortoiseSVN -----> Show log 2.右键点击你想撤回的提交 -> Revert changes from this revision ----->Rev ...
- asp.net:mv4 FileResult在IE8中下载不显示文件名和扩展名而显示Action方法名了!
IE8下,用户点击下载文件,会发现文件类型失丢的问题,解决方案如下: //IE8下载时,只显示action的名字,没有文件名和后缀 @仰止网Simba //return File(bufferbyte ...
- ijkplayer之.so文件编译过程
本文转载:ijkplayer编译so库真没那么难: 1.如何使用ijkplayer 官方:https://github.com/Bilibili/ijkplayer build.gradle添加下述依 ...
- c# HashTable (哈希表)
HashTable 哈希表 也是System.Collections集合下的数据结构类 它储存的也是Object类型的对象 但是它在内存中是散列排布的 因为这个特性,非常适合存储大量的数据 在Hash ...
- ActiveMQ(2)---ActiveMQ原理分析之消息发送
持久化消息和非持久化消息的发送策略 消息同步发送和异步发送 ActiveMQ支持同步.异步两种发送模式将消息发送到broker上.同步发送过程中,发送者发送一条消息会阻塞直到broker反馈一个确认消 ...
- (5)修改Linux的基本配置
**IP地址配置,最简单的方法:在命令行运行setup,按照提示修改即可. 1.修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=serv ...