当前位置:网站首页>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;
}
};边栏推荐
- Minimize the error
- ACM tutorial - quick sort (regular + tail recursion + random benchmark)
- BiLSTM-CRF代码实现
- No converter found for return value of type: class
- [eight sorting ③] quick sorting (dynamic graph deduction Hoare method, digging method, front and back pointer method)
- 站在新的角度来看待产业互联网,并且去寻求产业互联网的正确方式和方法
- [IVX junior engineer training course 10 papers to get certificates] 03 events and guessing numbers games
- ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)
- How to extract login cookies when JMeter performs interface testing
- [IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
猜你喜欢

Sql--- related transactions

SQL injection for Web Security (2)

一名优秀的软件测试人员,需要掌握哪些技能?

How to reflect and solve the problem of bird flight? Why are planes afraid of birds?
![[eight sorts ②] select sort (select sort, heap sort)](/img/4b/da0d08230391d6ee48cd8cfd2f7240.png)
[eight sorts ②] select sort (select sort, heap sort)

Iclr2022 | spherenet and g-spherenet: autoregressive flow model for 3D molecular graph representation and molecular geometry generation

学习笔记25--多传感器前融合技术

Advanced skills of testers: a guide to the application of unit test reports

【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)

Since I understand the idea of dynamic planning, I have opened the door to a new world
随机推荐
2022 low voltage electrician examination questions and answers
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
How to determine whether the current script is in the node environment or the browser environment?
Global and Chinese markets for supply chain strategy and operation consulting 2022-2028: Research Report on technology, participants, trends, market size and share
Two TVs
How does schedulerx help users solve the problem of distributed task scheduling?
Datawhale community blackboard newspaper (issue 1)
Bilstm CRF code implementation
AIX存储管理之逻辑卷的创建及属性的查看和修改
LeetCode、3无重复最长子序列
"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
Recently, three articles in the nature sub Journal of protein and its omics knowledge map have solved the core problems of biology
Picture puzzle wechat applet source code_ Support multi template production and traffic master
Evolution of Himalayan self-developed gateway architecture
【八大排序②】选择排序(选择排序,堆排序)
Cookie, session, tooken
学习笔记25--多传感器前融合技术
Bubble Sort Graph
Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
8.8.4-PointersOnC-20220215