当前位置:网站首页>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;
}
};
边栏推荐
- The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
- Leetcode --- longest public prefix
- 数学分析_笔记_第6章:一元函数的Riemann积分
- The light of ideal never dies
- 618深度複盤:海爾智家的制勝方法論
- Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
- 理想之光不灭
- Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
- What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
- 2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
猜你喜欢
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
注册成为harmonyos开发者并安装DevEco Studio 3.0 Beta2 for HarmonyOS
After the win10 system is upgraded for a period of time, the memory occupation is too high
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
Understand the key technology of AGV -- the difference between laser slam and visual slam
Add user-defined formula (time sharing t+0) to mobile app access as an example
数学分析_笔记_第5章:一元微分学
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
How to solve the failure of printer driver installation of computer equipment
随机推荐
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
Mysql database mysqldump why there is no statement to create a database
Yyds dry inventory executor package (parameter processing function)
unity Hub 登录框变得很窄 无法登录
What if the win11 app store cannot load the page? Win11 store cannot load page
Cloud native cicd framework: Tekton
Maui learning road (III) -- in depth discussion of winui3
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
618 deep resumption: Haier Zhijia's winning methodology
By asp Net core downloads files according to the path exception
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
sim2real环境配置教程
Summary of monthly report | list of major events of moonbeam in June
理想之光不灭
虚假的暑假
Multi data source configuration code
JS learning notes - first acquaintance
Maui学习之路(三)--Winui3深入探讨