当前位置:网站首页>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 <( ̄︶ ̄)>
边栏推荐
- 使用Scrapy框架爬取网页并保存到Mysql的实现
- 【深度学习】语义分割实验:Unet网络/MSRC2数据集
- How to understand that binary complement represents negative numbers
- 众昂矿业:萤石继续引领新能源市场增长
- Monthly observation of internet medical field in May 2022
- 最安全的证券交易app都有哪些
- Points for attention in porting gd32 F4 series programs to gd32 F3 series
- 【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
- [Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
- Guangzhou Development Zone enables geographical indication products to help rural revitalization
猜你喜欢

Cocos creator collision and collision callback do not take effect

How to release NFT in batches in opensea (rinkeby test network)

【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)

众昂矿业:萤石继续引领新能源市场增长

【搞船日记】【Shapr3D的STL格式转Gcode】

【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

#HPDC智能基座人才发展峰会随笔

Keil5 does not support online simulation of STM32 F0 series

Unity之ASE实现全屏风沙效果
随机推荐
Oracle control file loss recovery archive mode method
Typescript release 4.8 beta
STM32F103C8T6 PWM驱动舵机(SG90)
How to release NFT in batches in opensea (rinkeby test network)
./ Functions of configure, make and make install
Keil5 does not support online simulation of STM32 F0 series
Cut ffmpeg as needed, and use emscripten to compile and run
Bye, Dachang! I'm going to the factory today
Connecting FTP server tutorial
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
简述keepalived工作原理
知否|两大风控最重要指标与客群好坏的关系分析
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
The difference between full-time graduate students and part-time graduate students!
避坑:Sql中 in 和not in中有null值的情况说明
webgl_ Graphic transformation (rotation, translation, zoom)
大表delete删数据导致数据库异常解决
全日制研究生和非全日制研究生的区别!
MySQL bit类型解析
有钱人买房就是不一样