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

只遍历一次字符串即可求出最长不重复子串的长度。
int lengthOfLongestSubstring(string s) {
vector<int> charhash(,-); //记录字符上一次出现的位置,ASCII共有256个
int start=-; //当前所在不重复子串的起始位置的上一个位置
int maxlen=;
for(int i=;i<s.size();i++)
{
if(charhash[s[i]]>start) //如果该字符在当前子串中出现过
start=charhash[s[i]]; //开始新的子串
charhash[s[i]]=i; //更新字符位置
maxlen=max(maxlen,i-start); //每遍历一个字符,都更新一下最长不重复子串长度
}
return maxlen;
}
这种解法其实蕴含的就是动态规划的思想,只是写法非常的简便。
这道题动态规划的思路是:
遍历字符串的每一个字符i:
1. 若从当前不重复子串的起始位置开始,没有字符与i相同,则字符i可以添加进当前不重复子串,且当前不重复子串长度加1
2. 若从当前不重复子串的起始位置开始,字符i已经出现过,那么可以开始考察一个新的不重复子串,该子串包含当前字符i,且其长度为i-before
状态方程如下:

dp表示当前包含字符i的不重复子串的长度。before表示字符i上一次出现的位置,lastIndex是上一个不重复子串的起始位置。
*如果题目中涉及重复元素,就可以考虑用hash。
本题详细的分析,还可以参考:http://www.ahathinking.com/archives/123.html
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] 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 ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
随机推荐
- MFC DestroyWindow窗口对象和窗口句柄的销毁
考虑单窗口情况: 假设自己通过new创建了一个窗口对象pWnd,然后pWnd->Create.则销毁窗口的调用次序: 1. 手工调用pWnd->DestroyWindow(): 2. De ...
- Xcopy参数介绍
DOS批处理命令,永远是不朽的命令,不仅功能强大,同时,速度也是最快的!但是,很多新手学习计算机,都已经遗忘了本不该忘记的批处理命令. 我们不可数典忘祖,该学习的还是要学习,不该忘记的还是不能忘记,尤 ...
- Can you find it? 分类: 二分查找 2015-06-10 19:55 5人阅读 评论(0) 收藏
Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...
- Impala 3、Impala、Hbase整合
Impala可以通过Hive外部表方式和HBase进行整合,步骤如下: • 步骤1:创建hbase 表,向表中添加数据 create 'test_info', 'info' put 'test_inf ...
- vector之妙用系列
vector用法: 总结了下大家写的,感觉用着很方便: vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是 ...
- 让浏览器非阻塞加载javascript的几种方式
通常大多数浏览器是并行下载资源的,但由于外部脚本的特殊性例如通过脚本改变文档的DOM结构.脚本之间的存在依赖关系.使用document.write 向页面输出HTML等.浏览器为了确保正确执行脚本和呈 ...
- Windows7中安装内存与可用内存不一致的解决办法
转载:http://blog.sina.com.cn/s/blog_56741a420100h9d1.html 问题现象: 安装完Windows7后,在计算机->属性中,会看到安装内存.但有时在 ...
- Oracle创建数据库、表、用户
create tablespace south_knowledge logging datafile 'D:\TestDatabase\south_knowledge.dbf' size 10m au ...
- HDU 5729 - Rigid Frameworks
题意: 对于一个由n*m个1*1的菱形组成可任意扭曲的矩形(姑且这么说),求添加斜线*(两种)让菱形变成正方形,使得整个矩形固定且无法扭曲的方案数. 分析: n*m的矩形有如下性质:( 平 ...
- QJson 的使用
下载 源码解压 https://github.com/flavio/qjson 复制 src 目录下所有 .h .cpp .hh 文件到项目目录 qjson,pro 文件添加 INCLUDEPATH ...