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

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. arm linux中添加开机启动

    微处理器:S5PV210操作系统:linux3.0.8 前言:    在产品中,基本上都要屏蔽arm开发板中linux系统的对外通信,只应该通过产品的相关APP做相关操作.    因此需要把该APP添 ...

  2. 一 VC2008环境中ICE的配置

    VC2008环境中ICE的配置 ICE 3.4.0的下载页面 http://www.zeroc.com/download_3_4_0.html 环境变量配置  1.Ice-3.4.0安装到c:\Ice ...

  3. Climbing Stairs 解答

    Question You are climbing a stair case. It takes n steps to reach to the top. Each time you can eith ...

  4. 【分割平面,分割空间类题】【HDU1290 HDU2050】

    HDU 2050 折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. iOS/iPhone 程序文件目录结构以及启动流程

    要想清晰的理解IOS应用程序的启动过程,毫无疑问需要深入了解一下ios应用程序的文件系统.一个ios应用程序都有一个属于自己沙盒(sandbox),应用沙盒就是文件系统目录,并且与文件系统的其他部分隔 ...

  6. DNS,ARP,RARP,NAT,WINS的作用和区别

    DNS 域名服务系统,是将域名(比如www.cnblogs.com)转成ip地址.arp 地址转换协议,是将ip地址转成mac地址(物理地址,可用ipconfig /all查看).rarp从mac转到 ...

  7. 【初级坑跳跳跳】[NULLException] findViewById() id 引用错误,导致空指针

    在学习Intent页面切换,几个页面切换,导致view id 写错,写成另一个xml里的id去了,导致空指针异常 setContentView(R.layout.activity_second); B ...

  8. juce viewport使用

    1.设置内容组件 void PropertyPanel::init() { messageWhenEmpty = TRANS("(nothing selected)"); addA ...

  9. Uncaught TypeError: Object [object Object] has no method 'live'

    $( selector ).live( events, data, handler );                // jQuery 1.3+$( document ).delegate( se ...

  10. AngularJS之Factory vs Service vs Provider

    原文  http://www.linuxeden.com/html/news/20140509/151538.html 当你初试 Angular 时,很自然地就会往 controller 和 scop ...