当前位置:网站首页>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;
}
};
边栏推荐
- Nuget打包并上传教程
- 2021 OWASP TOP 10 Vulnerability Guide
- Why do we need to sub-library and sub-table?
- Recommendation System - Recall Phase - 2013: DSSM (Twin Towers Model) [Embedding (Semantic Vector) Recall] [Microsoft]
- Message queue data storage MySQL table design
- OAuth2:四种授权方式
- UnityShader入门学习(一)——GPU与Shader
- DeepLab Series Learning
- 基于极限学习机(ELM)进行多变量用电量预测(Matlab代码实现)
- A detailed guide to simulating latency with SQL/JDBC
猜你喜欢

LeetCode二叉树系列——222.完全二叉树的节点个数

UnityShader入门学习(三)——Unity的Shader

The 232-layer 3D flash memory chip is here: the single-chip capacity is 2TB, and the transmission speed is increased by 50%

我把问烂了的MySQL面试题总结了一下

Architecture actual combat battalion module 8 message queue table structure design

以后面试官问你 为啥不建议使用Select *,请你大声回答他!

深入浅出边缘云 | 4. 生命周期管理

梅克尔工作室-第一次

Five dimensions to start MySQL optimization

MySQL 23 classic interviews hang the interviewer
随机推荐
Redis与分布式:主从复制
分成两栏后文字顺序混乱的问题解决【写期刊论文时】
常用工具命令速查表
Groupid(artifact id)
《微信小程序-进阶篇》Lin-ui组件库源码分析-Icon组件
NC | 斯坦福申小涛等开发数据可重复分析计算框架TidyMass
Asynchronous processing business using CompletableFuture
Sentinel热点参数限流
OpenCV测量物体的尺寸技能 get~
redhat/openssl generates a self-signed ca certificate and uses it
1-hour live broadcast recruitment order: industry leaders share dry goods, and enterprise registration is open丨qubit · point of view
模板与泛型编程值typelist实现
Spark学习(3)-Spark环境搭建-Standalone
五个维度着手MySQL的优化
[Pytorch] F.softmax() method description
OAuth2:微服务权限校验Session共享
Small test knife: Go reflection helped me convert Excel to Struct
Getting started with UnityShader (3) - Unity's Shader
DeepLab Series Learning
Open Inventor 10.12 Major Improvements - Harmony Edition