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——数据类型转化 Data type conversion
类型转换 1.数字类型:int() | bool() | float() 2.str与int:int('10') | int('-10') | int('0') | float('-.5') | fl ...
- Android related
The build env. ensure that your computer’s BIOS is set up to support Intel’s virtualization extensio ...
- 深入理解CSS系列(一):理解CSS的盒子模型
接触前端也有好几个年头了,但是,讲实话,对于CSS的理解真的是不敢恭维,相信很多同行也有类似的感受吧!这是为什么呢?因为我们都认为CSS太简单了,没有必要深入学习,果真如此?其实,只不过是自己图样图森 ...
- liunx top命令详解
1,当前服务器时间,up,服务器离上一次重启过了多久,多少个用户在使用,cpu平均负载,grep 'core id' /proc/cpuinfo | sort -u | wc -l ,一般来说4个, ...
- C# 数据库
连接: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- asp.net core 基于角色的认证登陆
一.登陆页面的Controller [Authorize(Roles = "Admin,SuperAdmin")] public class ManageController : ...
- 关于Spring的那点事
一.Spring约束 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="htt ...
- [UE4]Image
一.Image.Appearance.Brush.Tiling:平铺方式 1.No Tile:不平铺,拉伸会变形 2.Horizontal:横向平铺.纵向拉伸会变形 3.Vertical:纵向平铺.横 ...
- TypeScript 模块系统
https://www.cnblogs.com/niklai/p/5808789.html
- python函数嵌套定义
python的这个特性是很特别的,与C#和C++都不一样.请看下面的例子 def outFun(): def innerFun_0():#1.在内部定义一个函数 print("i am fi ...