当前位置:网站首页>leetcode-3:无重复字符的最长子串
leetcode-3:无重复字符的最长子串
2022-07-05 05:46:00 【菊头蝙蝠】
leetcode-3:无重复字符的最长子串
题目
题目连接
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
解题
方法一:滑动窗口和哈希集合
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;
}
};
边栏推荐
- [cloud native] record of feign custom configuration of microservices
- 常见的最优化方法
- 用STM32点个灯
- SSH password free login settings and use scripts to SSH login and execute instructions
- 网络工程师考核的一些常见的问题:WLAN、BGP、交换机
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- [jailhouse article] performance measurements for hypervisors on embedded ARM processors
- [practical skills] technical management of managers with non-technical background
- Wazuh开源主机安全解决方案的简介与使用体验
- 26、 File system API (device sharing between applications; directory and file API)
猜你喜欢
Typical use cases for knapsacks, queues, and stacks
Hang wait lock vs spin lock (where both are used)
Solution to the palindrome string (Luogu p5041 haoi2009)
智慧工地“水电能耗在线监测系统”
Brief introduction to tcp/ip protocol stack
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Dichotomy, discretization, etc
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
剑指 Offer 53 - II. 0~n-1中缺失的数字
随机推荐
Simply sort out the types of sockets
26、 File system API (device sharing between applications; directory and file API)
Graduation project of game mall
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
SSH password free login settings and use scripts to SSH login and execute instructions
API related to TCP connection
Maximum number of "balloons"
用STM32点个灯
从Dijkstra的图灵奖演讲论科技创业者特点
PC寄存器
[practical skills] how to do a good job in technical training?
Developing desktop applications with electron
过拟合与正则化
884. Uncommon words in two sentences
Binary search template
Solution to game 10 of the personal field
AtCoder Grand Contest 013 E - Placing Squares
Personal developed penetration testing tool Satania v1.2 update
Sword finger offer 53 - I. find the number I in the sorted array
kubeadm系列-00-overview