当前位置:网站首页>2022.04.23(LC_763_划分字母区间)
2022.04.23(LC_763_划分字母区间)
2022-04-23 18:51:00 【Leeli9316】
方法:贪心
class Solution {
public List<Integer> partitionLabels(String s) {
//记录每个字母出现的最后位置
int[] lastPos = new int[26];
for (int i = 0; i < s.length(); i++) {
lastPos[s.charAt(i) - 'a'] = i;
}
List<Integer> ans = new ArrayList<>();
int start = 0; //开始位置
int end = 0; //结束位置
for (int i = 0; i < s.length(); i++) {
//更新字符出现的最远位置
end = Math.max(end, lastPos[s.charAt(i) - 'a']);
if (i == end) {
ans.add(end - start + 1);
start = i + 1;
}
}
return ans;
}
}
版权声明
本文为[Leeli9316]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Leeli9316/article/details/124360610
边栏推荐
- 深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
- K210 serial communication
- 玻璃体中的硫酸软骨素
- Usage of functions decode() and replace() in SQL
- Practice of Druid SQL and security in meituan review
- Seata handles distributed transactions
- After CANopen starts PDO timing transmission, the heartbeat frame time is wrong, PDO is delayed, and CANopen time axis is disordered
- Summary of actual business optimization scheme - main directory - continuous update
- 中金财富怎么样?在上边开户安全吗
- ESP32 LVGL8. 1 - bar progress bar (bar 21)
猜你喜欢
CANopen STM32 transplantation
玻璃体中的硫酸软骨素
Ctfshow - web362 (ssti)
iptables -L执行缓慢
Machine learning practice - naive Bayes
[mathematical modeling] - analytic hierarchy process (AHP)
七、DOM(下) - 章节课后练习题及答案
ctfshow-web361(SSTI)
The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
listener. log
随机推荐
12个例子夯实promise基础
About the operation of unit file reading (I)
CANopen STM32 transplantation
listener. log
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
Eight bit binary multiplier VHDL
C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
Nacos作为服务配置中心实战
Excel intercept text
微搭低代码零基础入门课(第三课)
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
Practice of Druid SQL and security in meituan review
One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
ESP32 LVGL8. 1 - anim animation (anim 16)
Query the logistics update quantity according to the express order number
K210串口通信
QT error: no matching member function for call to ‘connect‘
MySQL statement
Click the input box to pop up the keyboard layout and move up