当前位置:网站首页>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;
}
};
边栏推荐
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
- SPI details
- The sum of the unique elements of the daily question
- Dynamic planning solution ideas and summary (30000 words)
- 7. Processing the input of multidimensional features
- [rust notes] 16 input and output (Part 2)
- leetcode-6109:知道秘密的人数
- LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
- [rust notes] 14 set (Part 1)
猜你喜欢
LVS简介【暂未完成(半成品)】
Sqlmap tutorial (II) practical skills I
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
Arduino 控制的 RGB LED 无限镜
实时时钟 (RTC)
R language [import and export of dataset]
Personal developed penetration testing tool Satania v1.2 update
Introduction et expérience de wazuh open source host Security Solution
Redis publish subscribe command line implementation
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
随机推荐
leetcode-1200:最小绝对差
Doing SQL performance optimization is really eye-catching
One question per day 1447 Simplest fraction
【Rust 笔记】14-集合(上)
Liunx starts redis
[rust notes] 14 set (Part 1)
【Rust 笔记】17-并发(下)
LeetCode 0107.二叉树的层序遍历II - 另一种方法
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
QQ computer version cancels escape character input expression
[jailhouse article] jailhouse hypervisor
2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
927. 三等分 模拟
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
Scope of inline symbol
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
6. Logistic model
1039 Course List for Student
Daily question 1984 Minimum difference in student scores