当前位置:网站首页>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;
}
};
边栏推荐
- Control unit
- [rust notes] 16 input and output (Part 1)
- The connection and solution between the shortest Hamilton path and the traveling salesman problem
- 【Rust 笔记】16-输入与输出(上)
- 1040 Longest Symmetric String
- Sqlmap tutorial (1)
- Shutter web hardware keyboard monitoring
- 数据可视化图表总结(二)
- Data visualization chart summary (I)
- A reason that is easy to be ignored when the printer is offline
猜你喜欢
MIT-6874-Deep Learning in the Life Sciences Week 7
【云原生】微服务之Feign自定义配置的记录
【实战技能】如何做好技术培训?
[cloud native] record of feign custom configuration of microservices
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Appium automation test foundation - Summary of appium test environment construction
redis发布订阅命令行实现
Individual game 12
API related to TCP connection
R language [import and export of dataset]
随机推荐
Dichotomy, discretization, etc
[practical skills] technical management of managers with non-technical background
Introduction to convolutional neural network
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Over fitting and regularization
【实战技能】如何做好技术培训?
Shutter web hardware keyboard monitoring
[rust notes] 16 input and output (Part 1)
从Dijkstra的图灵奖演讲论科技创业者特点
Smart construction site "hydropower energy consumption online monitoring system"
SQLMAP使用教程(二)实战技巧一
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
Daily question 2013 Detect square
Doing SQL performance optimization is really eye-catching
leetcode-556:下一个更大元素 III
leetcode-1200:最小绝对差
【Rust 笔记】15-字符串与文本(下)
CPU内核和逻辑处理器的区别
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
一些工具的记录2022