当前位置:网站首页>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
边栏推荐
- Nacos作为服务注册中心
- Iptables - L executes slowly
- CANopen STM32 transplantation
- 从技术体系到商业洞察,中小研发团队架构实践之收尾篇
- ctfshow-web362(SSTI)
- Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
- Database computer experiment 4 (data integrity and stored procedure)
- Dynamically add and delete layouts
- ESP32 LVGL8. 1 - input devices (input devices 18)
- CISSP certified daily knowledge points (April 19, 2022)
猜你喜欢
From technical system to business insight, the closing chapter of the practice of small and medium-sized R & D team structure
How to virtualize the video frame and background is realized in a few simple steps
Tangle
Druid SQL和Security在美团点评的实践
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Druid SQL和Security在美团点评的实践
Introduction to ROS learning notes (I)
使用晨曦记账本,分析某个时间段每个账户收支结余
On iptables
随机推荐
七、DOM(下) - 章节课后练习题及答案
One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
CANopen STM32 transplantation
22年字节跳动飞书人力套件三面面经
Use of kotlin collaboration in the project
os_authent_prefix
Download xshell 6 and xftp6 official websites
ESP32 LVGL8. 1 - label (style 14)
Can filter
Tangle
Seata处理分布式事务
Excel intercept text
Ucosiii transplantation and use, reference punctual atom
Promote QT default control to custom control
Loop path
: app: transformclasseswithrobustfordevrease meituan hot repair compilation error record
机器学习理论基础篇--关于机器学习的一些术语
ESP32 LVGL8. 1 - bar progress bar (bar 21)
Practice of Druid SQL and security in meituan review
Introduction to QT programming