当前位置:网站首页>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
边栏推荐
- os_ authent_ Prefix
- Sentinel规则持久化进Nacos
- Nacos cluster construction and MySQL persistence configuration
- ESP32 LVGL8. 1 - bar progress bar (bar 21)
- ESP32 LVGL8. 1 - checkbox (checkbox 23)
- Simple use of viewbinding
- How can programmers quickly develop high-quality code?
- Go 语言 GUI 框架 fyne 中文乱码或者不显示的问题
- Deeply understand what new and make in golang are and what are the differences?
- MVVM model
猜你喜欢

微搭低代码零基础入门课(第三课)

os_authent_prefix

ESP32 LVGL8. 1 - roller rolling (roller 24)

ctfshow-web362(SSTI)

One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)

Summary of actual business optimization scheme - main directory - continuous update

Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système

Introduction to micro build low code zero Foundation (lesson 3)

机器学习理论之(8):模型集成 Ensemble Learning

MySQL Téléchargement et installation de la version Linux
随机推荐
Ctfshow - web362 (ssti)
Use of kotlin collaboration in the project
22年字节跳动飞书人力套件三面面经
使用晨曦记账本,分析某个时间段每个账户收支结余
Summary of actual business optimization scheme - main directory - continuous update
关于unity文件读取的操作(一)
iptables初探
Methods of nested recycleview to solve sliding conflict and incomplete item display
22 year flying Book manpower Kit
How to virtualize the video frame and background is realized in a few simple steps
ESP32 LVGL8. 1 - input devices (input devices 18)
RPM package management
深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
Download xshell 6 and xftp6 official websites
Practice of Druid SQL and security in meituan review
Configure iptables
纠结
七、DOM(下) - 章节课后练习题及答案
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Nacos as service registry