当前位置:网站首页>【LeetCode】Day110-划分字母区间
【LeetCode】Day110-划分字母区间
2022-08-07 06:48:00 【倒过来是圈圈】
题目
题解
根据提示里的内容可以做出个大半了,中心思想就是贪心
Try to greedily choose the smallest partition that includes the first letter. If you have something like “abaccbdeffed”, then you might need to add b. You can use an map like “last[‘b’] = 5” to help you expand the width of your partition.
class Solution {
public List<Integer> partitionLabels(String s) {
int n=s.length();
List<Integer>res=new ArrayList<>();
//字符最后出现的位置
int[] last=new int[26];
for(int i=0;i<n;i++){
last[s.charAt(i)-'a']=i;
}
int i=0;
while(i<n){
int end=last[s.charAt(i)-'a'];
for(int j=i+1;j<=end;j++){
end=Math.max(end,last[s.charAt(j)-'a']);
}
res.add(end-i+1);
i=end+1;
}
return res;
}
}
时间复杂度: O ( n ) O(n) O(n),看着是双重循环,但是 i 和 j 其实可以视为一个指针,共同遍历字符串s一次,所以复杂度其实只是O(n)
空间复杂度: O ( S ) O(S) O(S)
边栏推荐
- 10 years of experience summary: 7 tools for data analysts, focus on causal analysis!
- LNK2001 无法解析的外部符号 cuGetErrorName解决
- VoLTE Basic Self-Learning Series | Enterprise Voice Network Brief
- PriorityQueue(优先队列)
- TRACE32 - Memory Fill Test Data.Pattern
- TRACE32——变量显示选项Setup.Var
- In-depth analysis of HyperBDR cloud disaster recovery 2: Self-developed Boot in Cloud technology to achieve highly automated cloud disaster recovery
- 为什么NIO比BIO效率高
- grid grid layout
- LeetCode's sword is Offer 06. Print the linked list from end to end
猜你喜欢

Top 20 most popular plugins for QGIS

【井字棋】

A Pursuit of Temporal Accuracy in General Activity Detection TAG论文阅读笔记

2022A Special equipment related management (elevator) special work permit test question bank simulation test platform operation

微信小程序--》小程序全局配置和详解下拉刷新和上拉触底页面事件

VoLTE基础自学系列 | VoLTE终端哪些场景会触发CSFB?

神经网络ppt不足之处怎么写,神经网络ppt免费下载

NIO学习

GBL210-ASEMI机箱电源适配器整流桥GBL210

好消息|Erda 加入中国开源社区 landscape
随机推荐
Unity Shader: Blend混合
VoLTE基础自学系列 | 什么是SIP和IMS中的Forking
VoLTE Basic Self-Learning Series | Enterprise Voice Network Brief
路由交换综合实验
How to use the @Async annotation
VoLTE Basic Self-Learning Series | IMS Network Overview
用户登录模块---Druid+JDBC+Servlet
明明加了唯一索引,为什么还是产生重复数据?
OC-run loop
MySQL - index optimization
VoLTE基础自学系列 | 企业语音网简述
[acwing周赛复盘] 第 63 场周赛20220806
LNK2001 无法解析的外部符号 cuGetErrorName解决
Linear Regression in Machine Learning - Based on R
Detailed explanation of fixture test fixture of pytest framework
leetcode 110. 平衡二叉树
【Tic Tac Toe Chess】
TRACE32——Step
LeetCode 628. Maximum Product of Three Numbers
servlet 教程 2:返回 jsp 页面