当前位置:网站首页>Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters
Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters
2022-07-28 02:17:00 【Xiao Hao, who wants to enter Dachang】
Description of the stem :
Given a string s , Please find out There are no duplicate characters Of Longest substrings The length of .
LeetCode Original link
https://leetcode.cn/problems/longest-substring-without-repeating-characters/
Catalog
Method 2: Optimize sliding windows with hash sets
Method 1: The sliding window
Method 1 analysis :
This is the classic The sliding window Type of topic , There is almost a fixed routine : Determine an interval , Perform some operations on this interval , Then change the position and size of the interval through operation , To solve the problem .
Specific to this problem, the above is : Set the pointer i Traverse from the beginning , stay i If there was a connection with i The same characters ( Name it same), Then the length of the non repeating string becomes same The next character of to i The length of . The next cycle only needs to start from same The next character of Start judging , namely same+1 To i ( there i yes i++ After that i ) This interval .
The code is as follows :
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int size = s.size();
int MaxLen = 0; // Record the maximum length
int same=0; // Used to find in i Before subscript and s[i] The same characters
int begin=0; // Record the first position of the non repeating string
for (int i = 0; i < size; i++) // Every cycle is recorded with i The length of the longest non repeating string at the end
{
for(same = begin ; same < i ; same++)
{
if(s[same]==s[i])
{
begin=same+1;
break;
}
}
MaxLen=max(MaxLen,i-begin+1); // Record to i The length of the non repeating string at the end of the subscript
}
return MaxLen;
}
};Complexity analysis :
Time complexity :
, Two layers of circulation .
Spatial complexity :
, Only a constant space is opened .
Method 2: Optimize sliding windows with hash sets
Method 2 analysis :
Method 1 In, we use a loop to find whether there are the same elements before , This kind of use Loop search The time complexity of the method is O(N), It's easy to think of using unordered_set( Hash set ) Can make “ Find whether there is the same element before ” The time complexity of this matter becomes O(1), Is a typical use of Space for time How to do it . But the whole Execute the process need Minor modifications , see notes that will do .
The code is as follows :
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int size = s.size();
unordered_set<char> lookup; // Create a hash set
int MaxLen = 0; // Record the maximum length
int end = 0; // Mark the last position of the non repeating string
for (int i = 0; i < size; i++) //i Used to mark the first position of a non repeating string , Every cycle is recorded with i The length of the longest non repeating string at the beginning
{
while (end < size && lookup.count(s[end]) == 0)
{
lookup.insert(s[end]); // If there are no repeated characters before , Then add this character to the set , And make end Move backward 1 position
end++;
}
MaxLen = max(MaxLen, end - i);
lookup.erase(s[i]); // Delete s[i], That is to i The length of the longest non repeating string at the beginning has been calculated
}
return MaxLen;
}
};Complexity analysis :
Time complexity :
, Subscript i And subscripts end Traverse the string respectively , The loop is executed 2N Time .
Spatial complexity :
,unordered_set opened O(N) Space .
That's all for sharing , I'm not very talented , There must be mistakes or thoughtlessness , If there are corrections or suggestions , Welcome to leave a message in the comment area , I will reply immediately after seeing it and make appropriate changes to the article after thinking , Thank you for reading ~
边栏推荐
- 微信小程序图片根据屏幕比例缩放
- MySQL pymysql operation
- 实际工作中,我是如何使用 Postman 做接口测试?
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
- Principle and implementation of cross entropy
- Likeshop takeout ordering system [100% open source, no encryption]
- Flex layout learning completed on PC side
- Appium 点击操作梳理
- 软件测试面试题:你认为做好测试用例设计工作的关键是什么?
- [website construction] update SSL certificate with acme.sh: change zerossl to letsencrypt
猜你喜欢

二叉树的遍历和性质

都在说DevOps,你真正了解它吗?

LeetCode 热题 HOT 100 -> 2.两数相加

Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further

Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements

每条你收藏的资讯背后,都离不开TA

ArcGIS: loading historical remote sensing images

Skywalking distributed system application performance monitoring tool - medium

微信小程序实现动态横向步骤条的两种方式

synchronized详解
随机推荐
Data output - dynamic drawing
Sample imbalance - entry 0
损失函数-交叉熵的原理及实现
软件测试面试题:为什要在一个团队中开展测试工作?
Common video resolution
Promise from introduction to mastery (Chapter 2 understanding and use of promise)
Vxe Table/Grid 单元格分组合并
cn+dt
Wechat applet pictures are scaled according to the screen scale
C # using ABP warehouse to access the database error record set
synchronized详解
每条你收藏的资讯背后,都离不开TA
借助Elephant&nbsp;Swap打造的ePLATO,背后的高溢价解析
Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements
[Star Project] small hat aircraft War (V)
Promise从入门到精通(第3章 自定义(手写)Promise)
软考 --- 数据库(2)关系模型
MySQL create stored procedure ------ [hy000][1418] this function has none of deterministic, no SQL
[机缘参悟-53]:阳谋立身,阴谋防身
Codeworks round 810 (Div. 2) a~c problem solution
https://leetcode.cn/problems/longest-substring-without-repeating-characters/