当前位置:网站首页>LeetCode3_ Longest substring without duplicate characters
LeetCode3_ Longest substring without duplicate characters
2022-07-07 15:42:00 【WhiteTian】
Original article , Reprint please indicate the source .
Topic type : secondary
C++ Explain Addition of two numbers
The title is as follows
Given a string , 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 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" Is a subsequence , Not substring .
Example 4:
Input : s = ""
Output : 0
Tips :
0 <= s.length <= 5 * 104
s By the English letters 、 Numbers 、 Symbols and spaces
solution
Ideas :
The main idea of this problem is : The sliding window
What is a sliding window ?
It's actually a queue , For example, in the example abcabcbb, Enter this line ( window ) by abc Meet the requirements of the topic ,
When you enter a, The queue becomes abca, Not meeting the requirements at this time . therefore , We're going to move this queue !
How to move ?
All we have to do is move the elements on the left side of the queue , Until the subject requirements are met !
Keep this line up all the time , Find out the maximum length of the queue , Find out the solution !
Time complexity :O(n)
The code implementation is as follows
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// String length
int nLen = s.length();
// Character hash set , The same characters are not allowed
unordered_set<char> chardata;
// Index started , Maximum length
int nStart = -1, nMax = 0;
for(int i = 0; i < nLen; i++)
{
// The sliding window , Every time remove Drop the first character
if(i != 0)
{
//leetcode It must be written in this way :chardata.erase(s[i-1]), use //chardata.erase(chardata.begin()); incorrect .
chardata.erase(s[i-1]);
//VC++ In the environment //chardata.erase(chardata.begin()); Yes.
//chardata.erase(chardata.begin());
}
// Add non repeating characters in turn
while(nStart+1 < nLen && chardata.count(s[nStart+1]) == 0)
{
chardata.insert(s[nStart+1]);
++nStart;
}
// Calculate the maximum length
nMax < chardata.size() ? nMax = chardata.size() : nMax;
// Check whether the maximum length under the current cycle has been reached
if(nMax == nLen-i) break;
}
return nMax;
}
};



leetcode score 
thank you , It's not easy to create , Great Xia, please stay … Move your lovely hands , Give me a compliment before you go <( ̄︶ ̄)>
边栏推荐
- Steps to create P8 certificate and warehousing account
- Basic knowledge sorting of mongodb database
- Gd32 F3 pin mapping problem SW interface cannot be burned
- STM32F103C8T6 PWM驱动舵机(SG90)
- Clang compile link ffmpeg FAQ
- 【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
- Nacos一致性协议 CP/AP/JRaft/Distro协议
- 【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
- The download button and debug button in keil are grayed out
- Briefly describe the working principle of kept
猜你喜欢

Ida Pro reverse tool finds the IP and port of the socket server

简述keepalived工作原理

Iterator and for of.. loop

Annexb and avcc are two methods of data segmentation in decoding

如何在opensea批量发布NFT(Rinkeby测试网)
![[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)](/img/78/29eb8581e9a8fb4c6c7e1e35ad7adc.png)
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)

webgl_ Graphic transformation (rotation, translation, zoom)

【深度学习】图像超分实验:SRCNN/FSRCNN
![[server data recovery] data recovery case of raid failure of a Dell server](/img/5d/03bc8dcc6e554273b34a78c49a9eaf.jpg)
[server data recovery] data recovery case of raid failure of a Dell server

The difference between full-time graduate students and part-time graduate students!
随机推荐
How to create Apple Developer personal account P8 certificate
Window环境下配置Mongodb数据库
Points for attention in porting gd32 F4 series programs to gd32 F3 series
大表delete删数据导致数据库异常解决
What is Base64?
Mathematical modeling -- what is mathematical modeling
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
如何在opensea批量发布NFT(Rinkeby测试网)
Basic knowledge sorting of mongodb database
The difference between full-time graduate students and part-time graduate students!
Qu'est - ce qu'une violation de données
[deep learning] image hyperspectral experiment: srcnn/fsrcnn
jacoco代码覆盖率
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
What are PV and UV? pv、uv
[target detection] yolov5 Runtong voc2007 data set
Implementation of crawling web pages and saving them to MySQL using the scrapy framework
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation