当前位置:网站首页>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 <( ̄︶ ̄)>
边栏推荐
- [server data recovery] a case of RAID data recovery of a brand StorageWorks server
- [target detection] yolov5 Runtong voc2007 data set
- Unity之ASE实现卡通火焰
- The rebound problem of using Scrollview in cocos Creator
- Briefly describe the working principle of kept
- 【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
- A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
- Share the technical details of super signature system construction
- TypeScript 发布 4.8 beta 版本
- 避坑:Sql中 in 和not in中有null值的情况说明
猜你喜欢

Summer safety is very important! Emergency safety education enters kindergarten

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
![[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)](/img/d3/cab8a1cba3c8d8107ce4a95f328d36.png)
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
Introduction of mongod management database method

【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
![[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)](/img/3f/40475f9f6e0fcd3f58c93164f65674.png)
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)

Unity's ASE realizes cartoon flame

【兰州大学】考研初试复试资料分享

【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码

【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
随机推荐
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
Cocos creator collision and collision callback do not take effect
How to deploy the super signature distribution platform system?
Cocos uses custom material to display problems
XMIND frame drawing tool
【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
Super signature principle (fully automated super signature) [Yun Xiaoduo]
什麼是數據泄露
OpenGL's distinction and understanding of VAO, VBO and EBO
HW初级流量监控,到底该怎么做
Oracle control file loss recovery archive mode method
Typescript release 4.8 beta
Matlab experience summary
Webgl texture
Using eating in cocos Creator
Create lib Library in keil and use lib Library
Share the technical details of super signature system construction
Write sequence frame animation with shader
How to release NFT in batches in opensea (rinkeby test network)