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

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. app行业发展趋势

    近日,移动开放平台发布了2014年第一季度App开发行业报告.报告中对目前国内app开发者的分布情况,个人开发者和企业开发者的开发领域,相应比例以及提交应用过程中出现的问题做出统计,为如何建立一个更好 ...

  2. BFS visit tree

    There are two ways to conduct BFS on tree. Solution 1 -- Given level Use recursion to find given lev ...

  3. 如何区分监督学习(supervised learning)和非监督学习(unsupervised learning)

    监督学习:简单来说就是给定一定的训练样本(这里一定要注意,样本是既有数据,也有数据对应的结果),利用这个样本进行训练得到一个模型(可以说是一个函数),然后利用这个模型,将所有的输入映射为相应的输出,之 ...

  4. linux 有名管道(FIFO)

    http://blog.csdn.net/firefoxbug/article/details/8137762 linux 有名管道(FIFO) 管道的缓冲区是有限的(管道制存在于内存中,在管道创建时 ...

  5. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  6. IOS 下雪动画

    #define SNOW_IMAGENAME @"snow" #define IMAGE_X arc4random()%(int)Main_Screen_Width #define ...

  7. Spring的IOC

    引用:http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念 ...

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

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

  9. 如何在美国公司写project plan 邮件--以hadoop安装和Mahout数据分析为例子

    Hi, XXX (boss name) Project Title:  Hadoop installation and Data analysis based on Mahout Deliverabl ...

  10. Java生成登陆时使用的图片验证码

    package com.ws.frame.utils; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; i ...