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 ...
随机推荐
- oracle 多级菜单查询 。start with connect by prior
select * from S_dept where CODE in(select sd.code from s_dept sd start with sd.code='GDKB' connect b ...
- Intellij idea 设置svn 父目录文件显示状态颜色
file-->setting-->version control
- SpringMVC与ajax相关知识练习与存档
参考文章(亲测有效): SpringMVC+ajax返回JSON串 jquery ajax例子返回值详解 AJAX $.ajax({url:路径,date:数据,}) //get请求(指定回调函数,参 ...
- android4.3 Bluetooth(le)分析之startLeScan分析
BluetoothAdapter.java中有low enery(le)的一些方法,android提供了这些方法,但源码中并未找到这些方法的调用之处.本文档主要分析这类方法的执行流程,来了解下le到底 ...
- 一个使用方便的对FMDB进行封装的框架和一个可以切应用图标的应用
框架的git地址 :https://github.com/Joker-King/JKDBModel 切割图标的应用地址 http://pan.baidu.com/s/1kVjflwr
- Frogger
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sit ...
- 理解em,rem以及rem的失效问题
在平常做网站写代码的时候一般都是使用px,在之前的学习时就略微的学习了一些关于em.rem的知识,但是由于一直没有用到过,所以几乎全部忘记了.今天在研究一些知识的时候用到了em,所以特意将学到的知识总 ...
- Map:containsKey、containsValue 获取Map集合的键值的 值
get(Object key) 返回与指定键关联的值: containsKey(Object key) 如果Map包含指定键的隐射,则返回true: containsValue(Object valu ...
- 文件和目录:access函数
access函数是按照实际用户ID和实际组ID进行访问权限测试的: #include <unistd.h> int access( const char *pathname, int mo ...
- 2014年IT界最具“钱”景的10类岗位
来自人力资源公司罗致恒富(Robert Half)的一组2014年薪资指导显示,在工程.软件开发和程序领域有一技之长的职工的薪资可能出现何种程度的增长.罗致恒富预计2014年薪资将平均上调3.7%,而 ...