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

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. POJ 2263 Heavy Cargo(Floyd + map)

    Heavy Cargo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3768   Accepted: 2013 Descr ...

  2. No module named MYSQLdb 问题解决

    问题描述: 报错:ImportError: No module named MySQLdb 对于不同的系统和程序有如下的解决方法: easy_install mysql-python (mix os) ...

  3. Selenium模块化

    概述 高内聚低耦合是软件设计的一个基本原则. 内聚:从功能角度来度量模块内的联系,一个好的内聚模块应当恰好做一件事.它描述的是模块内的功能联系. 耦合:各模块之间相互连接的一种度量,耦合强弱取决于模块 ...

  4. hibernate某些版本(4.3)下报错 NoSuchMethodError: javax.persistence.Table.indexes()

    其实本来没啥大问题,但到网上查的时候发现了一些误人子弟的说法,所以还是记下来吧. 现象: hibernate从低版本升级到某一个版本时(我们是升到4.3.10)时,在程序启动时会报错: java.la ...

  5. .NET防止SQL、JS、HTML注入

    /// <summary> /// 过滤标记 /// </summary> /// <param name="NoHTML">包括HTML,脚本 ...

  6. 线程间通信的三种方式(NSThread,GCD,NSOperation)

    一.NSThread线程间通信 #import "ViewController.h" @interface ViewController ()<UIScrollViewDel ...

  7. InvokeRequired方法和Invoke函数

    c#中禁止跨进程直接访问控件,为了解决这个问题,出现了InvokeRequried属性,当一个控件的InvokeRequried属性值为真时,说明有控件外的线程想要访问它.这时便会调用到Invoke方 ...

  8. 实现一个宽和高都是100像素的div可以用鼠标拖拽移动的效果

    html,body{ width:100%;height:100%;margin:0px;padding:0px; } #box{ width:100px;height:100px;backgroun ...

  9. linux 内核开发基础

    开发特点 不需要第三方库支持 使用GNU C 没有内存保护机制 杜绝浮点数 栈区固定 必须关注并发及同步 注意可移植性

  10. python-整理-面向对象

    python的类和perl的类有相似之处,类的方法的第一个参数是表示类的对象自己,相当于c#的this python中定义类 class person: ''示例类,人'' count=0; def ...