当前位置:网站首页>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;
}
};
边栏推荐
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- Acwing 4301. Truncated sequence
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
- 【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
- Time complexity and space complexity
- Control unit
- Scope of inline symbol
- Daily question 1342 Number of operations to change the number to 0
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- [cloud native] record of feign custom configuration of microservices
猜你喜欢

How to adjust bugs in general projects ----- take you through the whole process by hand

智慧工地“水电能耗在线监测系统”

Some common problems in the assessment of network engineers: WLAN, BGP, switch

剑指 Offer 35.复杂链表的复制

Wazuh開源主機安全解决方案的簡介與使用體驗

In this indifferent world, light crying

YOLOv5-Shufflenetv2

剑指 Offer 09. 用两个栈实现队列

Light a light with stm32

Sword finger offer 04 Search in two-dimensional array
随机推荐
注解与反射
Binary search template
浅谈JVM(面试常考)
Daily question 2006 Number of pairs whose absolute value of difference is k
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Sword finger offer 58 - ii Rotate string left
CF1634 F. Fibonacci Additions
The number of enclaves
Chapter 6 data flow modeling - after class exercises
Time of process
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
Solution to game 10 of the personal field
Introduction to convolutional neural network
Acwing 4301. Truncated sequence
Smart construction site "hydropower energy consumption online monitoring system"
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
Mysql database (I)
Developing desktop applications with electron
挂起等待锁 vs 自旋锁(两者的使用场合)
剑指 Offer 05. 替换空格