当前位置:网站首页>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;
}
};
边栏推荐
- Some common problems in the assessment of network engineers: WLAN, BGP, switch
- On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
- Common optimization methods
- Daily question 1984 Minimum difference in student scores
- Simply sort out the types of sockets
- CF1637E Best Pair
- [jailhouse article] jailhouse hypervisor
- Smart construction site "hydropower energy consumption online monitoring system"
- Daily question 2013 Detect square
猜你喜欢

In this indifferent world, light crying

剑指 Offer 53 - II. 0~n-1中缺失的数字

Educational Codeforces Round 116 (Rated for Div. 2) E. Arena

SAP method of modifying system table data
![[cloud native] record of feign custom configuration of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] record of feign custom configuration of microservices

YOLOv5-Shufflenetv2

Dichotomy, discretization, etc

Brief introduction to tcp/ip protocol stack

Sword finger offer 09 Implementing queues with two stacks

【Jailhouse 文章】Jailhouse Hypervisor
随机推荐
Reader writer model
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
Scope of inline symbol
个人开发的渗透测试工具Satania v1.2更新
Time of process
Developing desktop applications with electron
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Little known skills of Task Manager
Sword finger offer 06 Print linked list from beginning to end
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
剑指 Offer 05. 替换空格
[jailhouse article] look mum, no VM exits
Introduction et expérience de wazuh open source host Security Solution
剑指 Offer 35.复杂链表的复制
Cluster script of data warehouse project
Acwing 4300. Two operations
Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
Control unit
Pointnet++ learning
How many checks does kubedm series-01-preflight have