题目:

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. cocos2dx 字体描边方法介绍

    转载地址:http://www.taikr.com/group/2/thread/1606 关于cocos2dx 字体描边的实现,不考虑效果和效率的话,是有三种方式: ① 利用CCLabelTTF制作 ...

  2. 自动化测试CTS命令

    #!/sbin/sh chmod +x /system/bin/input i=0 while [ "$i" != "10" ] do #am instrume ...

  3. 读取Webpage表中的内容

    nutch将从网页中抓取到的信息放入hbase数据库中,默认情况下表名为$crawlId_webpage,但表中的内容以16进制进行表示,直接scan或者通过Java API进行读取均只能读取到16进 ...

  4. AFNetworiking与ASIHttpRequest对比

    在开发iOS应用过程中,如何高效的与服务端API进行数据交换,是一个常见问题.一般开发者都会选择一个第三方的网络组件作为服务,以提高开发效率和稳定性.这些组件把复杂的网络底层操作封装成友好的类和方法, ...

  5. Android 操作手机内置存储卡中的文件

    场景:需要读取指定文件的内容,此文件是手动存储到手机内置存储卡中的,且手机上不存在SD卡. 对于android通过activity提供的openFileOutput和openFileInput可以直接 ...

  6. Android使用xml中定义的动画效果

    Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.zqrl_out); animation.setFil ...

  7. tcpdump详细用法

    1.1 三种关键字     关于类型的关键字     第一种是关于类型的关键字,主要包括host,net,port, 例如 host 210.27.48.2,指明 210.27.48.2是一台主机,n ...

  8. hdu 5612 Baby Ming and Matrix games(dfs暴力)

    Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...

  9. fragment的入门DEMO

    效果图: 代码如下: 1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  10. 把Go程序变小的办法

    把Go程序变小的办法是: go build -ldflags “-s -w” (go install类似) -s去掉符号表(然后panic时候的stack trace就没有任何文件名/行号信息了, 这 ...