当前位置:网站首页>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;
}
};
边栏推荐
- 多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
- Daily question 1984 Minimum difference in student scores
- Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
- 开源存储这么香,为何我们还要坚持自研?
- 1996. number of weak characters in the game
- Introduction and experience of wazuh open source host security solution
- 【云原生】微服务之Feign自定义配置的记录
- 1.13 - RISC/CISC
- 2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
- Common optimization methods
猜你喜欢
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
做 SQL 性能优化真是让人干瞪眼
【实战技能】如何做好技术培训?
Full Permutation Code (recursive writing)
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Individual game 12
Introduction to LVS [unfinished (semi-finished products)]
[article de jailhouse] jailhouse hypervisor
Introduction et expérience de wazuh open source host Security Solution
Time of process
随机推荐
Implement a fixed capacity stack
6. Logistic model
The connection and solution between the shortest Hamilton path and the traveling salesman problem
7. Processing the input of multidimensional features
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Wazuh开源主机安全解决方案的简介与使用体验
leetcode-6111:螺旋矩阵 IV
[cloud native] record of feign custom configuration of microservices
Daily question 2013 Detect square
Sqlmap tutorial (1)
LaMDA 不可能觉醒吗?
leetcode-6108:解密消息
[rust notes] 16 input and output (Part 1)
智慧工地“水电能耗在线监测系统”
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Convolution neural network -- convolution layer
Binary search template
Common optimization methods
Daily question 2006 Number of pairs whose absolute value of difference is k
1040 Longest Symmetric String