当前位置:网站首页>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 .
边栏推荐
- 抽象类以及接口
- FPGA based: moving target detection (schematic + source code + hardware selection, available)
- Shell tool finalshell
- HR must ask questions - how to fight with HR (collected from FPGA Explorer)
- ML10 self study notes SVM
- STM32 检测信号频率
- UE5 landscape 换算 Nanite 转换方式及不支持 配合 Lumen及Lumen开启 Dynamic Mesh 使用方法
- 链表--------------------尾插法
- JUC collection class is unsafe
- 【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识
猜你喜欢
随机推荐
【软件工程之美 - 专栏笔记】23 | 架构师:不想当架构师的程序员不是好程序员
【Leetcode刷题】数组2——二分查找
Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
NoClassDefFoundError 处理
UE5 landscape 换算 Nanite 转换方式及不支持 配合 Lumen及Lumen开启 Dynamic Mesh 使用方法
链表--------------------尾插法
STM32 printf问题总结 semihosting microLIB理解
爬取表情包
#6898 变幻的矩阵 题解
[beauty of software engineering - column notes] 17 | what is the need analysis? How to analyze?
Multithreading and concurrency
Operating system interview questions
无符号右移
JVM内存结构
单链表面试题
[beauty of software engineering - column notes] 14 | project management tools: all management problems should be considered whether they can be solved by tools
markdown与Typora
Understanding of synchronized eight lock phenomenon
ArduinoIDE + STM32Link烧录调试
Zero basics FPGA (5): counter of sequential logic circuit design (with introduction to breathing lamp experiment and simple combinational logic design)









