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.

Tags: Hash Table, Two Pointers, String

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
//从前往后开始,后面一旦碰到『当前字符串中』前面出现过的字符,则直接跳到前面那个重复字符的下一个字符开始新字符串统计。
//既然是比较当前字符串,必须要有一个值来保存当前字符串从什么位置之后开始。可以初始化一个int curSubStartPosPre = -1表示
//一开始是从-1后面也就是0开始统计的,字符串长度就是后面的index-(-1),如1-(-1)=2表示当前字符串长度达到了2。 //重复出现的判断方法:构建一个以字符为键的数组,因为字符不超过256,可构建int lastAppearPos[256],存储每个字符上一次
//出现的位置,当扫描到一个字符时,查看数组中对应值lastAppearPos[字符],如果大于curSubStartPosPre,说明这个字符上一次
//出现位置在当前字符串中,也就意味着当前字符串出现重复。 //随着字符的移动,当前的长度为index-curSubStartPosPre。保存一个max来表示最大值,如果当前长度大于max则更新max。 //lastAppearPos[256]:本来是index对应字符,现在以字符为下标对应index,完成hash table的构建。因为直接比较的是字符,
//用hash table可以实现o(1)的复杂度。 int curSubStartPosPre = -;
int lastAppearPos[];
int max=;
memset(lastAppearPos, -, sizeof(lastAppearPos));
int i=;
while(i<s.length()){
if(lastAppearPos[s[i]] > curSubStartPosPre){
curSubStartPosPre = lastAppearPos[s[i]];
}
if(i-curSubStartPosPre > max){
max = i-curSubStartPosPre;
} lastAppearPos[s[i]] = i;
i++;
}
return max; }
};
 

LeetCode Algorithm 03_Longest Substring Without Repeating Characters的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  3. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  4. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

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

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

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

  6. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  7. [Algorithm] Longest Substring Without Repeating Characters?

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

  8. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  9. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

随机推荐

  1. 【2017 Multi-University Training Contest - Team 1 1001】Add More Zero

    [Link]: [Description] 让你求最大的k; 使得 10^k<=2^m-1 [Solution] 求出2^m-1的位数就好; [lg(2^m-1)] = lg(2^m) = m* ...

  2. 【玲珑杯 round#18 B】图论你先敲完模板

    [Link]:http://www.ifrog.cc/acm/problem/1146 [Description] [Solution] 设f[i]表示在第i个点休息的话最少需要的体力值; f[i]= ...

  3. PipeCAD之管道标准库PipeStd

    PipeCAD之管道标准库PipeStd eryar@163.com Key Words. PipeCAD, PipeStd, 管道设计软件,管件库 1. Introduction 前不久,两位老友徐 ...

  4. C#加减乘除

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. js插件---Amaze UI dialog如何使用

    js插件---Amaze UI dialog如何使用 一.总结 一句话总结:别人给你列出来的参考手册照着用先 1.在哪里去找插件参考资料或者使用手册(一般位置找不到的时候)? github上面啊,非常 ...

  6. 洛谷P2115 [USACO14MAR]破坏Sabotage

    题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...

  7. Kinect 开发 —— 面部追踪

    SDK1.5中新增了人脸识别类库:Microsoft.Kinect.Toolkit.FaceTracking使得在Kinect中进行人脸识别变得简单,该类库的源代码也在Developer Toolki ...

  8. 【习题 8-3 UVA - 12545】Bits Equalizer

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果1的个数第一个串比第2个串多. 那么就无解. 否则. 找几个位置去凑1 优先找'?'然后才是0的位置 剩余的全都用swap操作就 ...

  9. GCC中-fpic解惑(转载)

    参考: 1.<3.18 Options for Code Generation Conventions>2.<Options for Linking>3.<GCC -fP ...

  10. Android 多种方式正确的载入图像,有效避免oom

    图像载入的方式:        Android开发中消耗内存较多一般都是在图像上面.本文就主要介绍如何正确的展现图像降低对内存的开销,有效的避免oom现象. 首先我们知道我的获取图像的来源一般有三种源 ...