当前位置:网站首页>LeetCode 热题 HOT 100 -> 3. 无重复字符的最长子串
LeetCode 热题 HOT 100 -> 3. 无重复字符的最长子串
2022-07-28 00:49:00 【想进大厂的小皓同学】
题干描述:
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
LeetCode原题链接
https://leetcode.cn/problems/longest-substring-without-repeating-characters/
目录
方法1:滑动窗口
方法1分析:
这是经典的滑动窗口类型的题目,几乎是有固定套路:确定一个区间,对这个区间进行某些操作,然后通过操作改变区间的位置和大小,从而解决问题。
具体到这道题上面就是:设置指针 i 从头开始遍历,在 i 之前如果有与 i 相同的字符(命名为same),则无重复字符串的长度就变为 same 的下一个字符到 i的长度。下一趟循环也只需从same 的下一个字符开始判断,即 same+1 到 i (这里的 i 是 i++ 之后的 i )这个区间。
代码如下 :
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int size = s.size();
int MaxLen = 0; //记录最大长度
int same=0; //用来寻找在i下标之前与s[i]相同的字符
int begin=0; //记录无重复字符串的首位置
for (int i = 0; i < size; i++) //每一趟循环都是在记录以i结尾的最长无重复字符串的长度
{
for(same = begin ; same < i ; same++)
{
if(s[same]==s[i])
{
begin=same+1;
break;
}
}
MaxLen=max(MaxLen,i-begin+1); //记录以i下标结尾的无重复字符串的长度
}
return MaxLen;
}
};复杂度分析 :
时间复杂度:
,两层循环。
空间复杂度:
,只开了常数个空间。
方法2:用哈希集合来优化滑动窗口
方法2分析:
方法1中我们使用循环来查找是否在之前有相同元素,这种使用循环查找的方法时间复杂度为O(N),我们很容易想到使用unordered_set(哈希集合)便能够使“查找之前是否有相同元素”这件事情的时间复杂度变为O(1),是典型的用空间换时间的做法。但是整个的执行流程需要稍作修改,看注释即可。
代码如下:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int size = s.size();
unordered_set<char> lookup; //创建一个哈希集合
int MaxLen = 0; //记录最大长度
int end = 0; //标记无重复字符串的最后一个位置
for (int i = 0; i < size; i++) //i用来标记无重复字符串的首位置,每一趟循环都是在记录以i开头的最长无重复字符串的长度
{
while (end < size && lookup.count(s[end]) == 0)
{
lookup.insert(s[end]); //如果之前没有重复的字符,则将该字符加入集合中,并使end后移1位
end++;
}
MaxLen = max(MaxLen, end - i);
lookup.erase(s[i]); //删除s[i],即以i开头的最长无重复字符串的长度已经计算完成
}
return MaxLen;
}
};复杂度分析:
时间复杂度:
,下标 i 和下标 end 分别遍历了一遍字符串,循环执行了2N次。
空间复杂度:
,unordered_set开了O(N)的空间。
该题的分享就到这里,本人才疏学浅,肯定会有错误或考虑不周到的地方,如有纠错或建议,非常欢迎在评论区留言,我看见后会立刻回复并在思考后对文章作出合适的修改,感谢阅读~
边栏推荐
- Fluorite network, difficult to be a "lone brave"
- IT这个岗位,人才缺口百万,薪资水涨船高,上不封顶
- 【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例
- 53: Chapter 5: develop admin management service: 6: develop [admin administrator exit login, interface]; (one point: when we want to modify a value with a certain coding method, the new value should b
- [Taichi] draw a regular grid in Tai Chi
- Gbase 8C transaction ID and snapshot (IV)
- Flex布局学习完成PC端
- 微信小程序实现动态横向步骤条的两种方式
- Gbase 8C transaction ID and snapshot (III)
- Data security and privacy computing summit - provable security: Learning
猜你喜欢

In the era of great changes in material enterprises, SRM supplier procurement system helps enterprises build a digital benchmark for property procurement

Embedded classic communication protocol

N32l43x FLASH read \ write \ erase operation summary
![[Taichi] draw a regular grid in Tai Chi](/img/48/14e825562afa3ffba96296799617f7.png)
[Taichi] draw a regular grid in Tai Chi

The petrochemical industry is facing the tide of rising prices, and the digital dealer distribution system platform enables dealers and stores

Execute add migration migration and report build failed

go 学习01

样本不均衡-入门0

FreeRTOS kernel summary

执行 Add-Migration 迁移时报 Build failed.
随机推荐
微信小程序图片根据屏幕比例缩放
Sample imbalance - entry 0
Five basic data structures of redis
Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements
Game 302 of leetcode
【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例
一种比读写锁更快的锁,还不赶紧认识一下
What devices does devicexplorer OPC server support? This article has listed
Flex layout learning completed on PC side
Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses
Unittest unit test framework full stack knowledge
Shell regular and metacharacters
Software test interview questions: common post data submission methods
A letter to the user of qubu drawing bed
新零售业态下,零售电商RPA助力重塑增长
The level "trap" of test / development programmers is not a measure of one-dimensional ability
ArcGIS: loading historical remote sensing images
Unreal ue4.27 switchboard porting engine process
11-Django-基础篇-数据库操作
Fiddler mobile packet capturing agent settings (for Huawei glory 60s)
https://leetcode.cn/problems/longest-substring-without-repeating-characters/