当前位置:网站首页>LeetCode 3、无重复字符的最长子串
LeetCode 3、无重复字符的最长子串
2022-04-21 15:31:00 【亡于灬】
3、无重复字符的最长子串
1)题目描述
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
提示:
0 <= s.length <= 5 * 104s由英文字母、数字、符号和空格组成
2)分析
滑窗,双指针。第一个指针是子串头部,第二个指针是子串尾部。头部在一个位置不动,在首尾之间没有重复字符的时候,尾部持续向后移动。当尾指针下一个字符与子串内字符重复时,收尾指针同时向后滑动一个字符,判断子串内有无重复字符。若有,继续滑动一个字符;若无,则判断尾指针后一个字符与子串内是否重复,重复则继续滑动,不重复则尾指针向后移动。直到尾指针到达字符串尾部。返回整个过程中子串长度的最大值即可。
3)C++代码
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res=0;
int left=0,right=-1;
unordered_set<char> hashSet;
for(left=0;left<s.size();left++){
if(left)
hashSet.erase(s[left-1]);
while(right+1<s.size()&&!hashSet.count(s[right+1])){
hashSet.insert(s[right+1]);
right++;
}
res=res>(right-left+1)?res:(right-left+1);
}
return res;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124315859
边栏推荐
- 智慧园区数融通-数字化赋能运营管理平台解决方案
- AcWing 1788. Why do cows cross the road (simulation)
- Mysql的安装与卸载
- Easy language CeF3 get the source code returned by the request
- How to set start date query statement in SQL Server
- What is an email address? Easy to use email registration application
- Deltix round, summer 2021 D. take a guess
- JDBC和数据库连接池
- GLASS:用于子图表示学习的 GNN 标签技巧
- Applet introduction and development tools
猜你喜欢
随机推荐
【unity笔记】L2Unity Shader基础
红队技术-父进程伪装(MITRE ATT&CK框架:T1134)
Easy language CeF3 get the source code returned by the request
78页数字孪生+智慧楼宇解决方案
JDBC和数据库连接池
[binary search - medium] sword finger offer II 070 Sort numbers that appear only once in the array
49页石油石化行业信息化规划与数字化转型
百度云盘好用还是阿里云盘好用?
sed命令
AcWing 1854. Promotion count (Analog)
Ultimate doll 2.0 | observable practice sharing of cloud native PAAS platform
Application of food safety law and relevant laws in patent examination
105页数字孪生城市信息模型CIM平台建设技术方案
注册登录程序1
How to apply for corporate email? How to register email with mobile phone number?
Easy Language cef3 get request returned source
【时序】Reformer:局部敏感哈希(LSH)实现高效 Transformer 论文笔记
105 page digital twin city information model CIM platform construction technical scheme
Page 92 group informatization planning scheme enterprise application integration solution
Openharmony camera user driven framework








