当前位置:网站首页>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;
}
};边栏推荐
- How to compress video size while adding watermark with one click?
- Bubble Sort Graph
- The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
- 工作中非常重要的测试策略,你大概没注意过吧
- CEPH buffer yyds dry inventory
- [Chongqing Guangdong education] Tianshui Normal University universe exploration reference
- Iclr2022 | spherenet and g-spherenet: autoregressive flow model for 3D molecular graph representation and molecular geometry generation
- CTF daily question day45 sensor
- [conference resources] the Third International Conference on Automation Science and Engineering in 2022 (jcase 2022)
- Keepalived introduction and installation
猜你喜欢

With the acquisition of Xilinx, AMD is more than "walking on two legs" | Jiazi found

XMIND mind map

SSO single sign on implementation.

Creation of volume group for AIX storage management (I)

Datawhale community blackboard newspaper (issue 1)
![[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card](/img/99/53b0ae47bada8b0d4db30d0517fe3d.jpg)
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card

Sql--- related transactions

GL Studio 5 安装与体验

Otaku wallpaper Daquan wechat applet source code - with dynamic wallpaper to support a variety of traffic owners

Infiltration records of CFS shooting range in the fourth phase of the western regions' Dadu Mansion
随机推荐
Mathematics - feelings -20220215
How can programmers better plan their career development?
Hcip day 14 (MPLS protocol)
Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
Data visualization in medical and healthcare applications
How to extract login cookies when JMeter performs interface testing
[disease detection] realize lung cancer detection system based on BP neural network, including GUI interface
【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码
Part 29 supplement (XXIX) basis of ECMAScript
Liteos learning - first knowledge of development environment
Minimize the error
测试人进阶技能:单元测试报告应用指南
I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
SQL injection for Web Security (2)
Powerful calendar wechat applet source code - support the main mode of doing more traffic
Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master
Bubble Sort Graph
Global and Chinese market of aircraft MRO software 2022-2028: Research Report on technology, participants, trends, market size and share
Collection: comprehensive summary of storage knowledge
Basic usage of shell script