当前位置:网站首页>Leetcode scribble notes 763. Divide the letter range (medium)
Leetcode scribble notes 763. Divide the letter range (medium)
2022-07-29 06:24:00 【Lu Yi, twelve】
1. subject :
character string S It's made up of lowercase letters . We're going to divide this string into as many pieces as possible , The same letter can appear in at most one segment . Returns a list representing the length of each string fragment .
Example :
Input :S = "ababcbacadefegdehijhklij"
Output :[9,7,8]
explain :
The result is "ababcbaca", "defegde", "hijhklij".
Each letter appears in at most one segment .
image "ababcbacadefegde", "hijhklij" The division is wrong , Because the number of segments is small .
2. Code :
class Solution {
public:
vector<int> partitionLabels(string s) {
int last[26];
int length = s.size();
// Find the last position of each character
for(int i = 0; i < length; ++i){
last[s[i] - 'a'] = i;
}
int start =0, end = 0;
vector<int> sss; // Store partitioned data
for(int i = 0; i < length; ++i){
end = max(end, last[s[i] - 'a']);
if(end == i){
sss.push_back(end-start+1);
start = end + 1;
}
}
return sss;
}
};3. Their thinking :
Input :S = "ababcbacadefegdehijhklij"
Output :[9,7,8]
explain :
The result is "ababcbaca", "defegde", "hijhklij".
Combined with the above example !!
The same letter can only appear in the same segment , Also divide the number of strings as much as possible , Then the position of the last occurrence of each letter , It is possible to divide the position of the string ,( The position of the two divisions in the example is a and e The last place ) In the process of traversing a string , By comparing the last position of the characters that have been traversed , The most backward position must be the interception end ,( For example, in the first string , Letter a The last place where the appears is s[8],s[0] To s[8] The last positions of the letters contained between are s[8] Before ) And the end of the previous string is the beginning of the next string (s[8] Is the end of the first string ,s[9] Is the beginning of the next string ).
4. Code :
@ First step : Find the last position of each character , Stored in an array .
@ The second step : Define two int Type variable start and end, The initial values are all 0,start Used to store the beginning of a string ,end Used to store the farthest end of the string that has been traversed . Update every character you traverse end Value , When end When the value is exactly the current traversal position , It's time to intercept the string , Then the intercepted length is end - start + 1, And then put (end + 1) Assign a value to start, Repeat operation .
边栏推荐
- LeetCode #977.有序数组的平方
- FPGA based: moving target detection (supplementary simulation results, available)
- 【Leetcode刷题】数组3——分治
- 位运算学习笔记
- [beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness
- 爬虫Requests库的一些简单用法
- clickhouse 导入CSV失败 不报错但是无数据
- 【软件工程之美 - 专栏笔记】14 | 项目管理工具:一切管理问题,都应思考能否通过工具解决
- Joiner.on和stream().map联合使用技巧
- LeetCode #35.搜索插入位置
猜你喜欢

LeetCode #3.无重复字符的最长子串
![[beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?](/img/0c/71557fa00accb2e6d30c13dc7f8efb.png)
[beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?

ML7 self study notes

ML10 self study notes SVM

2022暑初二信息竞赛学习成果分享1

LeetCode #7.整数反转

LeetCode #9.回文数
![[beauty of software engineering - column notes] 16 | how to write project documents?](/img/52/70d66230679abae6ce26d3477a22f6.png)
[beauty of software engineering - column notes] 16 | how to write project documents?

LeetCode #26.删除有序数组中的重复项

一些工具,插件,软件链接分享给大家~
随机推荐
leetcode刷题笔记 452. Minimum Number of Arrows to Burst Balloons (Medium) 452.用最少数量的箭引爆气球(中等)
Based on stc51: schematic diagram and source code of four axis flight control open source project (entry-level DIY)
官方教程 Redshift 09 Camera
Number theory: proof that the maximum number that px+py cannot represent is pq-p-q
SimpleFOC+PlatformIO踩坑之路
[beauty of software engineering - column notes] 16 | how to write project documents?
官方教程 Redshift 08 Light
2022 spring recruit - Shanghai an road FPGA post Manager (and Lexin SOC interview)
FPGA based: moving target detection (schematic + source code + hardware selection, available)
From entry to soul: how to use tb6600 single chip microcomputer to control stepping motor with high precision (42/57)
LeetCode #977.有序数组的平方
Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图
唯美girls
Install MySQL from scratch (MySQL installation document - unzipped version)
Leetcode 1. sum of two numbers
JUC collection class is unsafe
【软件工程之美 - 专栏笔记】30 | 用好源代码管理工具,让你的协作更高效
ArduinoIDE + STM32Link烧录调试
STM32: mcnamu wheel tracking task (library function program code)
【Leetcode刷题】数组2——二分查找