当前位置:网站首页>leetcode-3:无重复字符的最长子串
leetcode-3:无重复字符的最长子串
2022-07-05 05:46:00 【菊头蝙蝠】
leetcode-3:无重复字符的最长子串
题目
题目连接
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
解题
方法一:滑动窗口和哈希集合
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res=INT_MIN;
unordered_set<char> set;
int left=0,right=0;
while(right<s.size()){
if(set.count(s[right])==0){
set.insert(s[right]);
right++;
}
else{
set.erase(s[left]);
left++;
}
res=max(res,right-left);
}
if(res==INT_MIN) return 0;
else return res;
}
};
边栏推荐
- YOLOv5-Shufflenetv2
- Graduation project of game mall
- Some common problems in the assessment of network engineers: WLAN, BGP, switch
- kubeadm系列-01-preflight究竟有多少check
- Annotation and reflection
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- 注解与反射
- 剑指 Offer 35.复杂链表的复制
- How to adjust bugs in general projects ----- take you through the whole process by hand
- Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
猜你喜欢
浅谈JVM(面试常考)
Typical use cases for knapsacks, queues, and stacks
【Jailhouse 文章】Jailhouse Hypervisor
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
【实战技能】非技术背景经理的技术管理
个人开发的渗透测试工具Satania v1.2更新
剑指 Offer 09. 用两个栈实现队列
API related to TCP connection
Gbase database helps the development of digital finance in the Bay Area
Individual game 12
随机推荐
Daily question 1984 Minimum difference in student scores
PC寄存器
Control Unit 控制部件
Sword finger offer 05 Replace spaces
Sword finger offer 53 - I. find the number I in the sorted array
二十六、文件系统API(设备在应用间的共享;目录和文件API)
Alu logic operation unit
剑指 Offer 06.从头到尾打印链表
Developing desktop applications with electron
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
每日一题-无重复字符的最长子串
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
Annotation and reflection
Convolution neural network -- convolution layer
Full Permutation Code (recursive writing)
Talking about JVM (frequent interview)
Some common problems in the assessment of network engineers: WLAN, BGP, switch
In this indifferent world, light crying
剑指 Offer 09. 用两个栈实现队列