当前位置:网站首页>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 <( ̄︶ ̄)>
边栏推荐
- webgl_ Enter the three-dimensional world (1)
- [server data recovery] data recovery case of raid failure of a Dell server
- What is Base64?
- Oracle控制文件丢失恢复归档模式方法
- Streaming end, server end, player end
- Connecting FTP server tutorial
- Pat grade a 1103 integer factorizatio
- 使用cpolar建立一个商业网站(2)
- Detailed explanation of Cocos creator 2.4.0 rendering process
- Bye, Dachang! I'm going to the factory today
猜你喜欢

MySQL bit类型解析

Starting from 1.5, build a microservice framework link tracking traceid

【深度学习】图像超分实验:SRCNN/FSRCNN
![[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)](/img/d3/cab8a1cba3c8d8107ce4a95f328d36.png)
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)

Summer safety is very important! Emergency safety education enters kindergarten

webgl_ Graphic transformation (rotation, translation, zoom)

TypeScript 发布 4.8 beta 版本

一大波开源小抄来袭

【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
![[Lanzhou University] information sharing of postgraduate entrance examination and re examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Lanzhou University] information sharing of postgraduate entrance examination and re examination
随机推荐
Introduction of mongod management database method
最安全的证券交易app都有哪些
Super signature principle (fully automated super signature) [Yun Xiaoduo]
How to deploy the super signature distribution platform system?
Getting started with webgl (2)
Monthly observation of internet medical field in May 2022
Window环境下配置Mongodb数据库
Runnable是否可以中断
Spin animation of Cocos performance optimization
众昂矿业:萤石继续引领新能源市场增长
HW初级流量监控,到底该怎么做
摘抄的只言片语
全日制研究生和非全日制研究生的区别!
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
Webgl texture
What are the safest securities trading apps
Do not use memset to clear floating-point numbers
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
OpenGL common functions
Guangzhou Development Zone enables geographical indication products to help rural revitalization