只遍历一次字符串即可求出最长不重复子串的长度。

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 最长不重复子串的更多相关文章

  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] 3.Longest Substring Without Repeating Characters 最长无重复子串

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

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

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

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

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

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

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

  8. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

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

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

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

随机推荐

  1. Unique Paths II 解答

    Question Follow up for "Unique Paths": Now consider if some obstacles are added to the gri ...

  2. Repeated DNA Sequences 解答

    Question All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...

  3. iOS 7 改变Status Bar 颜色

    Set the UIViewControllerBasedStatusBarAppearance to NO in the Info.plist. In ViewDidLoad method or a ...

  4. STL中的set容器的一点总结(转)

    STL中的set容器的一点总结 1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂 ...

  5. background-position 使用方法具体介绍

    语法: background-position : length || length background-position : position || position 取值: length  : ...

  6. [Redux] Generating Containers with connect() from React Redux (FooterLink)

    Code to be refactored: class FilterLink extends Component { componentDidMount() { const { store } = ...

  7. putty保持连接不自动段开

    经常在网上看到有人说自己利用putty工具登录服务器总是连接不上,这样的情况自己在刚接触putty时也遇到过.在 Connection 里面有个 Seconds between keepaliaves ...

  8. CSS学习笔记——盒模型,块级元素和行内元素的区别和特性

    今天本来打算根据自己的计划进行前端自动化的学习的,无奈早上接到一个任务需求需要新增一个页面.自从因为工作需要转前端之后,自己的主要注意力几 乎都放在JavaScript上面了,对CSS和HTML这方面 ...

  9. asp.net 获取当前url地址

    设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.jb5 ...

  10. SFTP信任公钥配置及JSCH库

    1.SFTP信用公钥配置 1.1 客户端生成密钥对 以DSA举例: ssh-keygen –t dsa 执行该命令后,在home/用户名/.ssh目录下,会生成id_dsa和id_dsa.pub两个文 ...