LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
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(最长不重复子串)的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
- Longest Substring Without Repeating Characters 最长不重复子串
只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...
随机推荐
- Ubuntu系统监控cpu memery 磁盘Io次数 IO速率 网卡 运行时间等信息的采集
实验室最近在做的项目要做ubuntu系统监控,要获得系统的一些信息并返回给web服务器. web服务器与ubuntu主机的通信我写的程序用的是socket,至于为什么不用java程序ssh到对应的主机 ...
- memalign vs malloc - 使用O_DIRECT参数open一个文件并读写
听说使用odirect参数打开文件时能够以扇区的单位进行读写. 于是open了一个块设备文件/dev/sdo,当然还要带上读写参数O_RDWR 然后进行读写时出错了. 找了一会发现问题根本在于读写的b ...
- 关于MATHAPP的测试
这是我个人的单元测试博客仅供参考,想了解更全面的信息可请见我们团队的博客http://www.cnblogs.com/xjy-gg/p/5422868.html 首先,从世界那里下载adt-bundl ...
- Json&Razor&控制器
JsonJson 属于JavaScript所以要书写在<script></script>中1.语法规则: 1.1:键值对 1.2:逗号分隔 1.3:花括号保存对象 1.4:方括 ...
- Android Studio安装genymotion模拟器
1.Genymotion的安装: Genymotion无疑是目前最快最好用的模拟器.官网下载地址:https://www.genymotion.com/ 先注册,然后下载,安装VirtualBox最简 ...
- C++堆栈生长方向
栈区:临时区 #include <iostream> using namespace std; #include <stdio.h> int main() { ; ; cout ...
- Source Depot 使用总结
MS使用的Source Depot方案,主要是控制软件版本,类似的软件有SVN等,Source Depot一般使用起来也比较方便,可以灵活的配置,只要有访问权限,就可以下到对应的源代码文件. SD使用 ...
- Google Chrome: Make the Bookmarks Bar Display as Icons Only
By reducing your bookmarks to show only the icons, you can access more of them from the Bookmarks ba ...
- [leetcode 23]Merge k Sorted Lists
1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- APUE1
[APUE]进程控制(上) 一.进程标识 进程ID 0是调度进程,常常被称为交换进程(swapper).该进程并不执行任何磁盘上的程序--它是内核的一部分,因此也被称为系统进程.进程ID 1是in ...