当前位置:网站首页>Leetcode, 3 repeatless longest subsequence
Leetcode, 3 repeatless longest subsequence
2022-07-02 01:16:00 【Soraku7】
3. Longest substring without repeating characters
Medium difficulty 7743 Switch to English to receive dynamic feedback
Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
Example 1:
Input : s = "abcabcbb"
Output : 3
explain : Because the longest substring without repeating characters is "abc", So its The length is 3.
Example 2:
Input : s = "bbbbb"
Output : 1
explain : Because the longest substring without repeating characters is "b", So its length is 1.
Example 3:
Input : s = "pwwkew" Output : 3 explain : Because the longest substring without repeating characters is"wke", So its length is 3. Please note that , Your answer must be Substring The length of ,"pwke"It's a Subsequence , Not substring .
The method used in this question is to slide the window , use C++ Medium set To maintain the
The main idea of the topic should be , Provide a string , To find the longest string that does not repeat
The general idea should be like this , Provide a window , Continuously insert the elements in the string backwards , Once the inserted element repeats , Just move the starting position of the window forward one bit , Keep recording the size of the window , Find the maximum
The sliding window here is explained by drawing
Here's an example acdfcf , It's easy to see , The longest substring should ( One of ) This is acdf , The length is 4

You will now a Put it in the window

At this time, the maximum length of the string is 1, namely maxsize = 1
because c There is no repetition in the window , So will c Also insert into the window 
here maxsize = 2 ,d It doesn't appear in the window , So will d Put it in the window 
here maxsize = 3 ,f Do not repeat in the window , take f Insert window 
Family , Here comes the key point ,c Appeared in the window , therefore , We need to move the left side of the window , Until there is no repetition in the window

take a Remove window , But if you will c Insert , Or repeat , So we continue to shrink the left side of the window


here size = 3 , maxsize = 4,maxsize Do not update
Now insert f

There are repeating elements in the window , So start narrowing the window from the left

here size = 2 , maxsize = 4 , Finally work it out , The longest non repeating substring should be 4
Understand the general idea , Start now coding Well
First, we need to judge the empty string , If the string is empty , Then return directly
if(s==""){
return 0;
}Do some initialization
int left =0,right =0;
int maxsize =0;
set<char> detect;Now let's insert the string , When the inserted character does not appear in the sliding window , Just insert it directly , And update maxsize
if(detect.find(s[right])==detect.end()){
detect.insert(s[right]);
if(detect.size()>maxsize){
maxsize = detect.size();
}
}It's used here set In container find , Used to find whether there are the same elements in the container , If not, go back to end( end )
, If so, return the location of the same element .
When the same element is found , We should now move the left side of the sliding window , Until there is no repetition
for(right =0;right<s.size();right++){
while(detect.find(s[right])!= detect.end()){
detect.erase(s[left]);
left++;
}Finally, put the complete code , I hope I can help you
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;
}
};边栏推荐
- Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
- Zak's latest "neural information transmission", with slides and videos
- MySQL application day02
- Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
- Basic usage of shell script
- Since I understand the idea of dynamic planning, I have opened the door to a new world
- Cookie, session, tooken
- Advanced skills of testers: a guide to the application of unit test reports
- AIX存储管理之总结篇
- Day 13 of hcip (relevant contents of BGP agreement)
猜你喜欢

Powerful calendar wechat applet source code - support the main mode of doing more traffic

I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
![[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)](/img/0d/22f3f65ab9422383df9a55d0724d59.jpg)
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)

【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)

How to compress video size while adding watermark with one click?
![[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)

Xinniuniu blind box wechat applet source code_ Support flow realization, with complete material pictures

AIX存储管理之逻辑卷的创建及属性的查看和修改

Excel search and reference function

Day 13 of hcip (relevant contents of BGP agreement)
随机推荐
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
MySQL application day02
Mitsubishi PLC FX3U pulse axis jog function block (mc_jog)
Mathematics - feelings -20220215
Global and Chinese market of safety detection systems 2022-2028: Research Report on technology, participants, trends, market size and share
SSO single sign on implementation.
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
969 interlaced string
[conference resources] the Third International Conference on Automation Science and Engineering in 2022 (jcase 2022)
Review notes of compilation principles
Daily work and study notes
PLC Analog input analog conversion FB s_ ITR (Mitsubishi FX3U)
Variables and constants of go language foundation
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
Edge extraction edges based on Halcon learning_ image. Hdev routine
[IVX junior engineer training course 10 papers to get certificates] 03 events and guessing numbers games
[IVX junior engineer training course 10 papers] 06 database and services
Hcip day 14 (MPLS protocol)
Evolution of Himalayan self-developed gateway architecture
Just using the way and method of consuming the Internet to land and practice the industrial Internet will not bring long-term development