当前位置:网站首页>LeetCode 3. 无重复字符的最长子串
LeetCode 3. 无重复字符的最长子串
2022-07-02 13:15:00 【_刘小雨】
给定一个字符串 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) {
unordered_map<char, int> hash;
int re = 0;
/// j 在后面,i在前面,从i和j之间找最长子串,当发现之间的字符数量>1 ,i需要向后移动
for(int i = 0, j = 0; j < s.size(); j++)
{
hash[s[j]] ++;
while(hash[s[j]] > 1) hash[s[i++]] --;
re = max(re, j- i + 1);
}
return re;
}
};
边栏推荐
- Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
- 做机器视觉哪个软件好?
- Recalling the college entrance examination and becoming a programmer, do you regret it?
- Library management system (Shandong Agricultural University Curriculum Design)
- Yyds dry inventory company stipulates that all interfaces use post requests. Why?
- SSM整合-异常处理器及项目异常处理方案
- Summary of monthly report | list of major events of moonbeam in June
- PyC file decompile
- Leetcode -- number of palindromes
- 图书管理系统(山东农业大学课程设计)
猜你喜欢
PCL 点云镜像变换
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
PyC file decompile
Remove the underline in router link
Figure database | Nepal graph v3.1.0 performance report
mysql min() 求某条件下最小的值出现多个结果
Set the background picture in the idea (ultra detailed)
原神2.6服务端下载以及搭建安装教程
JS learning notes - variables
JS learning notes - operators
随机推荐
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
TypeScript数组乱序输出
Set the background picture in the idea (ultra detailed)
Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...
ROW_NUMBER()、RANK()、DENSE_RANK区别
Summary of monthly report | list of major events of moonbeam in June
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
Where can I open computer administrator permissions
Multi task prompt learning: how to train a large language model?
Leetcode --- longest public prefix
源码look me
[Xiaobai chat cloud] suggestions on container transformation of small and medium-sized enterprises
Written by unity Jason
SSM整合-异常处理器及项目异常处理方案
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
MySQL min() finds the minimum value under certain conditions, and there are multiple results
Multi data source configuration code
JS learning notes - operators