当前位置:网站首页>2022.04.23 (lc_763_divided into letter interval)
2022.04.23 (lc_763_divided into letter interval)
2022-04-23 18:53:00 【Leeli9316】

Method : greedy

class Solution {
public List<Integer> partitionLabels(String s) {
// Record the last position of each letter
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; // Starting position
int end = 0; // End position
for (int i = 0; i < s.length(); i++) {
// Update the farthest position where the character appears
end = Math.max(end, lastPos[s.charAt(i) - 'a']);
if (i == end) {
ans.add(end - start + 1);
start = i + 1;
}
}
return ans;
}
}
版权声明
本文为[Leeli9316]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231851339617.html
边栏推荐
- Practice of Druid SQL and security in meituan review
- 2022.04.23(LC_714_买卖股票的最佳时机含手续费)
- STM32: LCD display
- Coolweather is revised and connected to the wind weather interface to realize the broken line diagram of temperature
- 七、DOM(下) - 章节课后练习题及答案
- 实战业务优化方案总结---主目录---持续更新
- Practice of Druid SQL and security in meituan review
- 解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
- Yyds dry goods inventory stringprep --- Internet string preparation
- About the operation of unit file reading (I)
猜你喜欢
随机推荐
7、 DOM (Part 2) - chapter after class exercises and answers
Excel intercept text
从技术体系到商业洞察,中小研发团队架构实践之收尾篇
ESP32 LVGL8. 1 - img picture (IMG 20)
RPM package management
Usage of functions decode() and replace() in SQL
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
mysql_linux版本的下载及安装详解
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
配置iptables
The type initializer for ‘Gdip‘ threw an exception
: app: transformclasseswithrobustfordevrease meituan hot repair compilation error record
ESP32 LVGL8. 1 - arc (arc 19)
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
Yyds dry goods inventory stringprep --- Internet string preparation
ESP32 LVGL8. 1. Detailed migration tutorial of m5stack + lvgl + IDF (27)
使用晨曦记账本,分析某个时间段每个账户收支结余
Introduction to micro build low code zero Foundation (lesson 3)
Practice of Druid SQL and security in meituan review
RPM包管理








