题目:

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.

Tag:

String; Hash Table; Two Pointers

体会:

这道题对我来说挺难,看了很多答案,还是不太会。。。现在也只是知道了个大概意思。

大致的意思就是,首先我们要有一个Map, 其中<Key, Value>=<char, char在string s 中最后一次出现的位置>。然后开始逐个检查string中的char。

假设string s = "cxyzabdecb", 现在我们的substring是"abd",那么有两种情况我们可以直接增加当前substring的长度,一个是现在的char是第一次出现,(比如出现了e,那么就可以变成abde);另一个是虽然这个char在先前出现过,但是并没有在当前substring中出现过,比如abde后面的那个c, 判断条件是 map[s[i]] < i - curLen,意思就是前面那个c出现在abdec这个范围之外。综上,两种情况的共同特点都是char要在当前的substring中没有出现过。

除了上面两种情况,那么就是char在现在的substring中出现过了,那我们的substring就只能保留后来的这个部分了,即从上一次这个char后面的那一位到当前位置。例子:abdec现在往后检查遇到了b,为了能把b加进来,就只能保留dec,变成decb了。

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
// map<char, last index that char appears in the string
map<char, int> indexes;
// length of current substring
int curLen = ;
// length of max substring length so far
int maxLen = ;
int n = s.length(); for (int i = ; i < n; i++) {
char ch = s[i];
// if this char appears the first time or
// is not part of the current substring
if ((indexes.count(s[i]) == ) ||
(curLen < i - indexes[s[i]])) {
curLen++;
} else {
curLen = (i - indexes[s[i]]);
}
// update
indexes[s[i]] = i;
maxLen = (curLen > maxLen) ? curLen : maxLen;
}
return maxLen;
}
};

[Leetcode] Longest Substring Without Repeating Characters (C++)的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. C++ leetcode Longest Substring Without Repeating Characters

    要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...

  5. [LeetCode]Longest Substring Without Repeating Characters题解

    Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  7. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  8. LeetCode——Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. sqlite编译

    1.下载代码:http://www.sqlite.org/download.html ,windows下下载sqlite-amalgamation-xxx.zip和sqlite-dll-win32-x ...

  2. 在windows中搭建php开发环境

    一.wampserver wampserver是一个安装集成包,包含了开发所需的apache,mysql,php,简单方便. 下载地址 http://www.xiazaiba.com/html/279 ...

  3. centos 源码安装git

    (1) 添加rpmforge源 wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686 ...

  4. Java开发工具与程序调试

    开发工具:MyEclipse,Eclipse等. 程序调试:  (1)断点:设置断点是程序调试中必不可少的手段,Java调试器每次遇到程序断点时都会将当前线程挂起,即暂停当前程序的运行.(在Eclip ...

  5. SqlServer计算周岁的函数

    CREATE Function Dbo.GetAge ( @birthday datetime, @now datetime ) Returns int As Begin Declare @Age i ...

  6. VS2010的调试参数/Zi /DEBUG

    /DEBUG只是是否要生成调试信息的开关.这个命令行选项在链接器页面那里 /Zi只是生成的调试信息的格式,这个格式是.pdb文件.当然还有好几种格式.这个命令行在编译页那里 如果上面的选项没有设置对, ...

  7. Qt编程之d指针与q指针

    我们在Qt中可以看到两个宏Q_D和Q_Q这两个红分别是取得d指针和q指针的,d指针指向封装的私有类,q指针指向公共的类.(我的理解类似于回调,回指的意思). 为什么Qt要这样实现呢?下面几个链接中的文 ...

  8. JS的substr与substring的区别

    substr返回从指定位置开始的指定长度的子字符串 str.substr(star[,length])  第二个参数可选,不选的话,截取到最后,如果length为0或者负数,那么返回的将是一个空字符串 ...

  9. 使用Windows USB-DVD制作U盘启动安装系统盘

    第一步:到如下所示的地址下载所需要的*.iso系统镜像文件. http://msdn.itellyou.cn/ 第二步:下载Windows USB-DVD工具 https://www.microsof ...

  10. 决策树之ID3算法实现(python)

    决策树的概念其实不难理解,下面一张图是某女生相亲时用到的决策树: 基本上可以理解为:一堆数据,附带若干属性,每一条记录最后都有一个分类(见或者不见),然后根据每种属性可以进行划分(比如年龄是>3 ...