当前位置:网站首页>Leetcode-3: Longest substring without repeated characters
Leetcode-3: Longest substring without repeated characters
2022-07-05 06:09:00 【Chrysanthemum headed bat】
leetcode-3: Longest substring without repeating characters
subject
Topic linking
Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
Example 1:
Input : s = "abcabcbb"
Output : 3
explain : Because the longest substring without repeating characters is "abc", So its length is 3.
Example 2:
Input : s = "bbbbb"
Output : 1
explain : Because the longest substring without repeating characters is "b", So its length is 1.
Example 3:
Input : s = "pwwkew"
Output : 3
explain : Because the longest substring without repeating characters is "wke", So its length is 3.
Please note that , Your answer must be Substring The length of ,"pwke" Is a subsequence , Not substring .
Problem solving
Method 1 : Sliding window and hash collection
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;
}
};
边栏推荐
- SPI details
- [rust notes] 16 input and output (Part 1)
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
- [rust notes] 16 input and output (Part 2)
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
- “磐云杯”中职网络安全技能大赛A模块新题
- leetcode-6109:知道秘密的人数
- 对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
- Full Permutation Code (recursive writing)
- leetcode-6111:螺旋矩阵 IV
猜你喜欢
![[jailhouse article] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[jailhouse article] jailhouse hypervisor

CF1637E Best Pair

Implement an iterative stack

Introduction et expérience de wazuh open source host Security Solution

7. Processing the input of multidimensional features

可变电阻器概述——结构、工作和不同应用
![R language [import and export of dataset]](/img/5e/a15ab692a6f049f846024c98820fbb.png)
R language [import and export of dataset]

Sqlmap tutorial (II) practical skills I

传统数据库逐渐“难适应”,云原生数据库脱颖而出

Appium基础 — 使用Appium的第一个Demo
随机推荐
开源存储这么香,为何我们还要坚持自研?
leetcode-6110:网格图中递增路径的数目
How to adjust bugs in general projects ----- take you through the whole process by hand
MIT-6874-Deep Learning in the Life Sciences Week 7
QQ computer version cancels escape character input expression
Open source storage is so popular, why do we insist on self-development?
[jailhouse article] look mum, no VM exits
Appium基础 — 使用Appium的第一个Demo
[rust notes] 16 input and output (Part 1)
AtCoder Grand Contest 013 E - Placing Squares
[rust notes] 14 set (Part 1)
One question per day 1447 Simplest fraction
Implement an iterative stack
Sqlmap tutorial (II) practical skills I
leetcode-31:下一个排列
【Jailhouse 文章】Jailhouse Hypervisor
R语言【数据集的导入导出】
CF1634 F. Fibonacci Additions
LeetCode 1200.最小绝对差
F - Two Exam(AtCoder Beginner Contest 238)