当前位置:网站首页>LeetCode、3无重复最长子序列
LeetCode、3无重复最长子序列
2022-07-02 01:10:00 【Soraku7】
难度中等7743收藏分享切换为英文接收动态反馈
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: s = "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是"wke",所以其长度为 3。 请注意,你的答案必须是 子串 的长度,"pwke"是一个子序列,不是子串。
本题使用的做法是滑动窗口,用C++中的set来维持
题目的大意应该是,提供一个字符串,要找到其中不重复的最长字串
大概思路应该是这样的,提供一个窗口,不断的向后插入字符串中的元素,一旦插入的元素重复,就将窗口的起始位置向前移动一位,不断记录窗口的大小,找到其中的最大值
这里的滑动窗口我用画图来进行解释
这里举个例子 acdfcf , 很容易看出,最长的子串应(之一)该为acdf , 长度为4

现在将a放入窗口

此时字串的最大长度为1,即maxsize = 1
因为c在窗口中没有重复,所以将c也插入窗口中
此时maxsize = 2 ,d在窗口中没有出现过,所以将d放入窗口中
此时maxsize = 3 ,f不在窗口中重复,将f插入窗口
家人们,关键点来了,c在窗口中出现过,因此,我们要移动窗口的左侧,直到窗口不再出现重复为止

将a移除窗口,可是如果将c插入,还是重复,所以我们继续将窗口左侧缩小


此时size = 3 , maxsize = 4,maxsize不进行更新
现在插入f

窗口中有重复元素,所以开始从左缩小窗口

此时size = 2 , maxsize = 4 , 最后算出来,最长不重复子串应该是4
了解了大概的思路,现在就开始coding吧
首先我们需要对空字符串进行特判,如果字符串为空,则直接返回
if(s==""){
return 0;
}进行一些初始化
int left =0,right =0;
int maxsize =0;
set<char> detect;现在我们进行对字符串的插入,当插入的字符没有在滑动窗口中出现过的时候,就直接进行插入,并且更新maxsize
if(detect.find(s[right])==detect.end()){
detect.insert(s[right]);
if(detect.size()>maxsize){
maxsize = detect.size();
}
}这里使用了set容器中的find ,用来寻找容器中是否有相同的元素,没有的话就返回end(末端)
,有的话就返回相同元素所在位置。
当找到相同元素的时候,我们现在应该移动滑动窗口的左侧,直到没有重复为止
for(right =0;right<s.size();right++){
while(detect.find(s[right])!= detect.end()){
detect.erase(s[left]);
left++;
}最后将完整代码放上 , 希望能帮助到大家
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s==""){
return 0;
}
int left =0,right =0;
int maxsize =0;
set<char> detect;
for(right =0;right<s.size();right++){
while(detect.find(s[right])!= detect.end()){
detect.erase(s[left]);
left++;
}
if(detect.find(s[right])==detect.end()){
detect.insert(s[right]);
if(detect.size()>maxsize){
maxsize = detect.size();
}
}
}
return maxsize;
}
};边栏推荐
- 什么是商业养老保险?商业养老保险安全靠谱吗?
- Review notes of compilation principles
- Deb file installation
- I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
- 程序员该如何更好的规划自己的职业发展?
- Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
- Bc35 & bc95 onenet mqtt (old)
- Otaku wallpaper Daquan wechat applet source code - with dynamic wallpaper to support a variety of traffic owners
- How does schedulerx help users solve the problem of distributed task scheduling?
- Global and Chinese markets for the application of artificial intelligence in security, public security and national security 2022-2028: Research Report on technology, participants, trends, market size
猜你喜欢

How to reflect and solve the problem of bird flight? Why are planes afraid of birds?

Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)

Datawhale community blackboard newspaper (issue 1)

To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"

excel查找与引用函数

About asp Net core uses a small detail of datetime date type parameter

How to extract login cookies when JMeter performs interface testing
![[conference resources] the Third International Conference on Automation Science and Engineering in 2022 (jcase 2022)](/img/a6/a2afdf9e18255c9171f61bf074998b.png)
[conference resources] the Third International Conference on Automation Science and Engineering in 2022 (jcase 2022)

【八大排序①】插入排序(直接插入排序、希尔排序)

首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
随机推荐
cookie、session、tooken
How does schedulerx help users solve the problem of distributed task scheduling?
Han Zhichao: real time risk control practice of eBay based on graph neural network
How to determine whether the current script is in the node environment or the browser environment?
Global and Chinese market of avionics systems 2022-2028: Research Report on technology, participants, trends, market size and share
什么是商业养老保险?商业养老保险安全靠谱吗?
Keepalived introduction and installation
[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
BiLSTM-CRF代码实现
To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"
Infiltration records of CFS shooting range in the fourth phase of the western regions' Dadu Mansion
Summary of Aix storage management
How does schedulerx help users solve the problem of distributed task scheduling?
SQL injection for Web Security (2)
Bc35 & bc95 onenet mqtt (old)
You probably haven't noticed the very important testing strategy in your work
MySQL winter vacation self-study 2022 12 (4)
Global and Chinese market of collaborative applications 2022-2028: Research Report on technology, participants, trends, market size and share
What is commercial endowment insurance? Is commercial endowment insurance safe?
Recently, three articles in the nature sub Journal of protein and its omics knowledge map have solved the core problems of biology