当前位置:网站首页>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;
}
};
边栏推荐
- The paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022
- OpenShift 4 - 定制 RHACS 安全策略,阻断生产集群使用高风险 Registry
- 【Pytorch】F.softmax()方法说明
- Node version switching management using NVM
- Redis与分布式:哨兵模式
- 使用 PyTorch 检测眼部疾病
- Network cable RJ45 interface pins [easy to understand]
- 以后面试官问你 为啥不建议使用Select *,请你大声回答他!
- Getting started with UnityShader (3) - Unity's Shader
- 49. The copy constructor and overloaded 】
猜你喜欢

OAuth2:四种授权方式

Message queue data storage MySQL table design
![MySQL [subquery]](/img/0b/9bbf54c500d85976e6d6776b6c6f13.png)
MySQL [subquery]

Nuget打包并上传教程

The meaning of node_exporter performance monitoring information collection in Prometheus

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

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

1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点

UnityShader入门学习(一)——GPU与Shader

OpenShift 4 - 用 Operator 部署 Redis 集群
随机推荐
NC | 中国农大草业学院杨高文组揭示发现多因子干扰会降低土壤微生物多样性的积极效应...
c语言hello world代码(代码编程入门)
NPM Taobao mirror (latest version) released a new version of npm mirror at 2021-11-21 16:53:52 [easy to understand]
charles进行弱网测试(app弱网测试怎么做)
Shell script classic case: backup of files
Redis与分布式:哨兵模式
学习笔记12--路径-速度分解法之局部路径搜索
/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件的作用
OpenShift 4 - 定制 RHACS 安全策略,阻断生产集群使用高风险 Registry
Shell project combat 1. System performance analysis
Shang Silicon Valley-JVM-Memory and Garbage Collection (P1~P203)
OAuth2:资源服务器
OAuth2:搭建授权服务器
分成两栏后文字顺序混乱的问题解决【写期刊论文时】
NC | 斯坦福申小涛等开发数据可重复分析计算框架TidyMass
leetcode: 485. Maximum number of consecutive 1s
“听我说谢谢你”还能用古诗来说?清华搞了个“据意查句”神器,一键搜索你想要的名言警句...
Uniapp WeChat small application reference standard components
[Blue Bridge Cup Trial Question 46] Scratch Magnet Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
Sentinel流量控制