当前位置:网站首页>763.划分字母区间——之打开新世界
763.划分字母区间——之打开新世界
2022-07-31 14:46:00 【empty__barrel】
763.划分字母区间——之打开新世界
题目:
字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。
题目要求:
- 尽可能多的片段
- 相同字母合成到一个片段内
思路流程:
①只需要记录相同字母的位置即可->②记录相同字母位置最大范围即可->③合并重叠区间
输入:S = “ababcbaca defegde hijhklij”
a:[1,9]
b:[2,6]
c:[5,8]
d:[10,15]
e:[11,16]
h:[17,20]
i: [18,23]
j: [19,24]
此时已完成第②步,
【1,9】,【2,6】,【5,8】合并成【1,9】
【10,15】,【11,16】合并成【10,16】
【17,20】,【18,23】,【19,24】合并成【17,24】
此时就是所需要的答案了吗,可以发现此题经过处理之后与前面所做的合成区间几乎一模一样。所以称之为打开新世界。
代码:过不了,写了这么多总不能不发吧!!!生气!
class Solution {
public:
static bool comparev(vector<int>& a, vector<int>& b){
return a[1]<b[1];
}
vector<int> partitionLabels(string s) {
vector<vector<int>> v;
for(int i = 0; i < s.size();++i){
//遍历string
if (v.size() == 0) {
//是否是入第一个元素
vector<int> v0;
v0.push_back(int(s[i])); //入字母
v0.push_back(0); //入位置
}
else {
for(int j = 0; j < v.size();++j){
//遍历result,当前字母是否存在
if(v[j][0] == int(s[i])){
if(v[j].size() == 2){
v[j].push_back(i);
}
else{
if(v[j][2] < i) v[j][2] = i;
}
}
else{
vector<int> v0;
v0.push_back(int(s[i])); //入字母
v0.push_back(i); //入位置
}
}
}
}
sort(v.begin(),v.end(),comparev);
vector<int> v1;
int begin = v[0][1];
int end = v[0][2];
for(int i = 1; i < v.size();i++){
if(v[i][1] < end){
if(v[i][2] > end){
end = v[i][2];
}
}
else{
v1.push_back(end-begin+1);
begin = v[i][1];
end = v[i][2];
}
}
v1.push_back(end-begin+1);
return v1;
}
};
代码实现比较复杂还没实现,后来看了力扣题解发现更简单方法
解析:
在脑海中想象出由一个个动态字符构成的字符串,将其划分为三个片段,第一个片段的起始值是第一个值,末尾值是这个片段中间的某个值的end,第二个片段的起始值是上个片段的end+1对应的那个值,end是这个片段中间的某个值的end,第三个片段类似。
代码:
class Solution {
public:
vector<int> partitionLabels(string s) {
int far[27]={
0};
for (int i = 0; i < s.length();++i){
//记录字符串中字母出现的最远下标
far[s[i]-'a'] = i;
}
//字母区间的合并同时记录区间长度
int start = 0;
int end = 0;
vector<int> v;
for(int i = 0 ; i < s.size(); ++i){
//筛选区间
end = max(end,far[s[i]-'a']);
if( i == end){
v.push_back(end-start+1);
start = i+1;
}
}
return v;
}
};
边栏推荐
- jOOQ 3.14 released - SQL/XML and SQL/JSON support
- In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!
- 蔚来杯2022牛客暑期多校训练营4
- The meaning of node_exporter performance monitoring information collection in Prometheus
- Unity Shader入门精要学习——透明效果
- 公告
- MANIFEST.MF文件(PDB文件)
- 看交互设计如何集成到Scrum敏捷流程中
- Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research
- 学习笔记12--路径-速度分解法之局部路径搜索
猜你喜欢
Redis与分布式:主从复制
Sentinel热点参数限流
NC | 斯坦福申小涛等开发数据可重复分析计算框架TidyMass
49. The copy constructor and overloaded 】
自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统
基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)
纸质说明书秒变3D动画,斯坦福大学吴佳俊最新研究,入选ECCV 2022
How to grab configuration information for DELL SC compellent storage system
看交互设计如何集成到Scrum敏捷流程中
LeetCode二叉树系列——222.完全二叉树的节点个数
随机推荐
Getting started with UnityShader (3) - Unity's Shader
为什么要分库分表?
Redis与分布式:集群搭建
NC | 中国农大草业学院杨高文组揭示发现多因子干扰会降低土壤微生物多样性的积极效应...
ERROR: Failed building wheel for osgeo
How to clean up the lodash.memoize cache in the element-plus virtual table virtual-list component?
Linux bash: redis-server: command not found
OAuth2:四种授权方式
IDEA连接MySQL数据库并使用数据
Sentinel热点参数限流
STM32(十)------- SPI通信
Jmeter常用的十大组件
sentinel与nacos持久化
Combination series - there are combinations when there are arrangements
ERROR: Failed building wheel for osgeo
我把问烂了的MySQL面试题总结了一下
MySQL [aggregate function]
Groupid(artifact id)
The JVM a class loader
【Pytorch】torch.argmax()用法