[LeetCode] 046: Longest Substring Without Repeating Characters[通俗易懂]

[LeetCode] 046: Longest Substring Without Repeating Characters[通俗易懂][Problem]Givenastring,findthelengthofthelongestsubstringwithoutrepeatingcharacters.Forexample,thelongestsubstringwithoutrepeatinglettersfor”abcabcbb”is”abc”,whichthelength

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

[Problem]

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

[Solution]

class Solution {
  
  
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int res = 0, i = 0, j = 0;
map<char, int> lastIndex;
while(j < s.size()){
if(lastIndex.find(s[j]) != lastIndex.end() && lastIndex[s[j]] >= i){
i = lastIndex[s[j]] + 1;
}
res = max(res, j - i + 1);
lastIndex[s[j]] = j;
j++;
}
return res;
}
};

说明:版权所有,转载请注明出处。
Coder007的博客

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191595.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 图解-使用【变异系数】赋予权重,并比较效果

    图解-使用【变异系数】赋予权重,并比较效果变异系数CoefficientofVariation用于比较多组数据的离散程度比较两组量纲不同的数据的离散程度,不能用标准差,可考虑变异系数不适用场景:数据下限小于0(导致平均值等于0)变异系数权重法使用变异系数计得的权重值会随着数据的变化而变化,可认为是一种无监督学习

    2022年5月27日
    52
  • POTPLAYER视频播放器及相当教程_播放器播放器什么是播放器

    POTPLAYER视频播放器及相当教程_播放器播放器什么是播放器原文出自:www.hangge.com  转载自原文链接:http://www.hangge.com/blog/cache/detail_1461.html#一、MADVR介绍MADVR 是一款超强的视频插件,其配合高清播放软件,可以做到目前 PC 上播放高清视频的最强画质。 MADVR 这款视频渲染器比市面上大多数播放器自带的渲染器有着更精确的颜色处理,更高质量的图像缩放缩放、以及更低…

    2022年9月14日
    0
  • oracle建表语句例子_oracle建表语句例子带注释

    oracle建表语句例子_oracle建表语句例子带注释Oracle建表语句createtabletable_name( idnumner(12), textverchar2(255CHAR)notnull,statusnumber(1)DEFAULT0null–添加默认值如果为空默认值就为0)–添加主键ALTERTABLE&amp;amp;amp;amp;quot;W…

    2022年9月7日
    0
  • settings官方网站_phpstorm中文

    settings官方网站_phpstorm中文setting —> php,选择php版本,并点击…,选择到php.exe进入到appserv底下找到php.ini文件,查找date.timezone,去掉前面的;号,添加”Asia/Shanghai”重启appserv环境,就是重启下apache 和 mysql服务发现依然失败后面重启电脑就可以了哈哈哈哈哈哈哈哈…

    2022年8月18日
    19
  • LockWorkStation in Windows 9X/ME/NT/2000

    LockWorkStation in Windows 9X/ME/NT/2000LockWorkStationinWindows9X/ME/NT/2000代码作者:Delphiscn(cnBlaster#sohu.com)http://blog.csdn.net/delphiscn程序原码:http://blog.csdn.net/delphiscn/archive/2005/06/25/403157.aspx在线下载:http://de1phiscn.bokee….

    2022年7月21日
    10
  • html5 sexteen,Teens ‘conservative’ about pre-marital sex

    html5 sexteen,Teens ‘conservative’ about pre-marital sexAsignificantmajorityofChineseteenagersarequiteconservativewhenitcomestopre-maritalsex,asurveyof1,500peoplefromadistrictofChongqingmunicipalityhasshown.Thelatestpollsurvey…

    2022年5月10日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号