当前位置:网站首页>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
- 迁移学习进阶
- One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
- Chondroitin sulfate in vitreous
- 2022.04.23(LC_714_买卖股票的最佳时机含手续费)
- Database computer experiment 4 (data integrity and stored procedure)
- Nacos作为服务注册中心
- Machine learning theory (8): model integration ensemble learning
- listener.log
- 【数学建模】—— 层次分析法(AHP)
猜你喜欢
解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
ESP32 LVGL8. 1 - checkbox (checkbox 23)
MySQL学习第五弹——事务及其操作特性详解
c#:泛型反射
MVVM模型
After opening the original normal project, the dependency package displays red and does not exist.
【数学建模】—— 层次分析法(AHP)
mysql_linux版本的下載及安裝詳解
Download xshell 6 and xftp6 official websites
STM32: LCD display
随机推荐
RPM package management
Treatment of incomplete display of listview height
Machine learning practice - naive Bayes
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
Click the input box to pop up the keyboard layout and move up
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
Excel intercept text
Configure iptables
Golang 语言实现TCP UDP通信
Query the logistics update quantity according to the express order number
迁移学习进阶
12 examples to consolidate promise Foundation
7、 DOM (Part 2) - chapter after class exercises and answers
Introduction to micro build low code zero Foundation (lesson 3)
Domestic GD chip can filter
ESP32 LVGL8. 1 - bar progress bar (bar 21)
ESP32 LVGL8. 1 - textarea text area (textarea 26)
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
Usage of functions decode() and replace() in SQL
SQL中函数 decode()与 replace()的用法