当前位置:网站首页>Leetcode 763. partition labels divide alphabetic intervals (medium)
Leetcode 763. partition labels divide alphabetic intervals (medium)
2022-07-29 04:41:00 【okokabcd】
One 、 The main idea of the topic
label : greedy
https://leetcode.cn/problems/partition-labels
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 .
Tips :
- S The length of is [1, 500] Between .
- S Contains only lowercase letters ‘a’ To ‘z’ .
Two 、 Their thinking
A string S, Split it into substrings as much as possible , The condition is that each character can only appear in one substring at most . In the example above , character string S There are multiple a, these a Must only be in the first substring , Letter e Appears in the second substring . The difficulty of this problem is how to find the breakpoint of the string , That is, the position of splitting into substrings . Look at the above examples , Once a letter appears many times , Then the last occurrence position must be in the current substring , Letter a,e,j They are the ending letters in the three substrings . So we focus on the last position of each letter , We can use a Map To establish the mapping between the letter and its last occurrence , After the mapping is established , You need to start traversing the string S, We maintain a start Variable , Is the starting position of the current substring , One more last Variable , Is the end position of the current substring , Whenever we traverse to a letter , Need to be in Map Extract its last position , Because once the current substring eats a letter , It must eat all the same letters , So we keep updating with the last position of the current letter last Variable , Only when i and last It's the same , namely i=last when , The current substring contains the last position of all the letters that have appeared , That is, the following string will not have the letters that appeared before , At this time, it should be the disconnected position , We add the substring length to the result .
3、 ... and 、 How to solve the problem
3.1 Java Realization
public class Solution {
public List<Integer> partitionLabels(String s) {
Map<Character, Integer> map = new HashMap<>();
char[] cArr = s.toCharArray();
for (int i = 0; i < cArr.length; i++) {
map.put(cArr[i], i);
}
List<Integer> res = new ArrayList<>();
int start = 0;
int last = 0;
for (int i = 0; i < cArr.length; i++) {
last = Math.max(map.get(cArr[i]), last);
if (i == last) {
res.add(last - start + 1);
start = last + 1;
}
}
return res;
}
}
Four 、 Summary notes
- 2022/7/28 Clear your mind , The code comes out naturally
边栏推荐
猜你喜欢
删除word文档中的空白页
Corresponding order of 18 and 25coco data of openpose and joint points
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
Record the Niua packaging deployment project
There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
Hengxing Ketong invites you to the 24th China expressway informatization conference and technical product exhibition in Hunan
Implementation of img responsive pictures (including the usage of srcset attribute and sizes attribute, and detailed explanation of device pixel ratio)
Unity Foundation (3) -- various coordinate systems in unity
谷歌浏览器 打开网页出现 out of memory
使用更灵活、更方便的罗氏线圈
随机推荐
Pytoch automatic mixing accuracy (AMP) training
Nail dialog text converted to pictures cannot be copied and pasted on the document
Simply change the picture color
There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
Ethernet of network
Install the gym corresponding to mujoco in the spinning up tutorial, and the error mjpro150 is reported
Basic operation of queue
ios面试准备 - 网络篇
Corresponding order of 18 and 25coco data of openpose and joint points
Coding questions encountered in the interview
[c language] PTA 7-63 falling ball
def fasterrcnn_resnet50_fpn()实例测试
Classes and objects (I)
After the spinning up installation is completed, use the tutorial to test whether it is successful. There are library "Glu" not found and 'from pyglet.gl import * error solutions
STL source code analysis (Hou Jie) notes -- Classification and testing of stl containers
Shell string segmentation
GCC基础知识
JVM (heap and stack) memory allocation
I++ and ++i details
正确的用户拖拽方式