当前位置:网站首页>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
边栏推荐
- Mysql各版本下载地址及多版本共存安装
- There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
- post导出数据,返回
- Hengxing Ketong invites you to the 24th China expressway informatization conference and technical product exhibition in Hunan
- Pyqt5 learning pit encounter and pit drainage (2) buttons in qformlayout layout cannot be displayed
- Mongo Shell交互式命令窗口
- [c language] PTA 7-51 sum the first n terms of odd part sequence
- Pyscript cannot import package
- iOS面试准备 - 其他篇
- 【Express连接MySQL数据库】
猜你喜欢
On quotation
STL source code analysis (Hou Jie) notes - STL overview
Corresponding order of 18 and 25coco data of openpose and joint points
Classes and objects (II)
ssm整合增删改查
[QT learning notes] * insert pictures in the window
MySQL - deep parsing of MySQL index data structure
恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会
【Express连接MySQL数据库】
JVM (heap and stack) memory allocation
随机推荐
Unity Foundation (3) -- various coordinate systems in unity
命令行交互工具(最新版) inquirer 实用教程
C语言之基础语法
央企建筑企业数字化转型核心特征是什么?
Install the gym corresponding to mujoco in the spinning up tutorial, and the error mjpro150 is reported
Pytorch fixed random seed & recurrence model
Basic grammar of C language
TypeError: Cannot read properties of undefined (reading ‘then‘)
Exception handling: pyemd or pyemd not found
Oracle update and delete data
pyscript无法引入包
Introduction to auto.js script development
redux快速上手
Pycharm reports an error when connecting to the virtual machine database
Oracle 插入数据
[C] PTA 6-8 finding the height of binary tree
Actual combat of flutter - DIO of request encapsulation (II)
On the use of pyscript (realizing office preview)
带你一文理解JS数组
【Express连接MySQL数据库】