当前位置:网站首页>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 2006 Number of pairs whose absolute value of difference is k
- Wazuh開源主機安全解决方案的簡介與使用體驗
- 【Rust 笔记】16-输入与输出(上)
- Implement a fixed capacity stack
- Flutter Web 硬件键盘监听
- Groupbykey() and reducebykey() and combinebykey() in spark
- Time of process
- Implement an iterative stack
- Spark中groupByKey() 和 reduceByKey() 和combineByKey()
- Typical use cases for knapsacks, queues, and stacks
猜你喜欢

快速使用Amazon MemoryDB并构建你专属的Redis内存数据库

MIT-6874-Deep Learning in the Life Sciences Week 7

Appium automation test foundation - Summary of appium test environment construction

1.15 - 输入输出系统

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

Sqlmap tutorial (1)

Open source storage is so popular, why do we insist on self-development?

SPI 详解
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

Sqlmap tutorial (II) practical skills I
随机推荐
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
【Jailhouse 文章】Jailhouse Hypervisor
SQLMAP使用教程(二)实战技巧一
QQ computer version cancels escape character input expression
927. 三等分 模拟
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
Flutter Web 硬件键盘监听
Some common problems in the assessment of network engineers: WLAN, BGP, switch
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
Collection: programming related websites and books
leetcode-9:回文数
The connection and solution between the shortest Hamilton path and the traveling salesman problem
One question per day 1447 Simplest fraction
【Rust 笔记】16-输入与输出(上)
Over fitting and regularization
1041 Be Unique
【Rust 笔记】15-字符串与文本(下)
Daily question 1688 Number of matches in the competition
Shutter web hardware keyboard monitoring
Dynamic planning solution ideas and summary (30000 words)