题目链接

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.


推荐参考博客:最长不重复子串

注意:输入空串时,返回0

以abcdbefdhij 为例:

图中的start是一个标志位,表示当前不重复子串的起始位置,图中的数字表示记录字符出现位置的数组hashtable,比如字符b出现在第1位,那么hashtable[‘b’]=1。                              本文地址

顺序扫描字符串,第4个位置时,在hashtable中发现b已经出现过(记出现的位置为k,此时k=1),那么当前的不重复子串长度 = 当前位置-start;下一个不重复子串就应该从第k+1个字符(2号位的c)开始,即令start = 2,并且把[start, k)位置的字符对应的hashtable清空,重新设置b在hashtable的位置为4。继续扫描直到再次发现相同的字符,和前面一样处理。注意全部处理完字符串,还要判断一下末尾的不重复子串是否是最长的。

时间复杂度分析:最坏情况下,相当于遍历了两遍字符串,因此时间复杂度是O(n)

class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> bitmap(128, -1);
int res = 0;
int start = 0, lastStart = 0;
for(int i = 0; i < s.size(); i++)
{
if(bitmap[s[i]] != -1)
{
res = max(res, i-start);
lastStart = start;
start = bitmap[s[i]] + 1;
for(int j = lastStart; j < bitmap[s[i]]; j++)
bitmap[s[j]] = -1;
}
bitmap[s[i]] = i;
}
res = max(res, (int)s.size()-start);//不要忘了最后的判断
return res;
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3736725.html

LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)的更多相关文章

  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 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

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

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

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

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

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

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

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

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

  7. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  8. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

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

  9. 003 Longest Substring Without Repeating Characters 最长不重复子串

    Given a string, find the length of the longest substring without repeating characters.Examples:Given ...

  10. Longest Substring Without Repeating Characters 最长不重复子串

    只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...

随机推荐

  1. 云计算和大数据时代网络技术揭秘(十七)VOQ机制

    VOQ机制 本章介绍的VOQ是一种新型的QoS机制,目的是为了解决著名的交换机HoL难题. 但VOQ强烈依赖于调度算法,例如,一个48口的交换机,每个端口都要维护48-1个FIFO缓存队列, 一共48 ...

  2. Linux更改服务器Hostname

    在我们需要维护较多的服务器时,有意义的Hostname将时刻提醒我们这台服务器的功能. ****** 1.Debian echo '127.0.1.1 git-server' >> /et ...

  3. 【枚举】bzoj3391 [Usaco2004 Dec]Tree Cutting网络破坏

    #include<cstdio> using namespace std; #define N 10001 int n; int v[N<<1],first[N],next[N ...

  4. SQL语句中如何把文件以二进制数组形式存入数据库

    System.IO.FileStream fs = new FileStream("1.txt", FileMode.Open); string strConn = "D ...

  5. sqlplus实现上下翻页设置

    环境: 操作系统:red hat 5.8 OS 数据库:oracle 11g 11.2.0.1 安装软件:IO-Tty-1.07.tar. Term-ReadLine-Gnu-1.16.tar.uni ...

  6. SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN

    1 trunc(value,precision)按精度(precision)截取某个数字,不进行舍入操作.2 round(value,precision)根据给定的精度(precision)输入数值. ...

  7. C#设计模式(14)——模板方法模式(Template Method)

    一.引言 提到模板,大家肯定不免想到生活中的“简历模板”.“论文模板”.“Word中模版文件”等,在现实生活中,模板的概念就是——有一个规定的格式,然后每个人都可以根据自己的需求或情况去更新它,例如简 ...

  8. quartznet笔记

    http://sourceforge.net/projects/quartznet/files/quartznet/

  9. Unity3D——相机跟随物体移动

    public Transform target; public float moveSmooth=5f; Vector3 offset; void Start () { offset = transf ...

  10. .Net免费公开课视频+资料+源码+经典牛逼 汇总篇【持续更新】

    博主推荐一:WP8.1最经典培训教程 博主点评:经典Windows Phone8.1 Runtime API培训最经典教程,此教程由传智播客蒋坤老师录制的一整套WP8.1入门级视频教程,讲授内容非常广 ...