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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

 

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

 

思路:

首先是判断有没有重复,用到Hash方法 – 散列。

然后这里的想法是用一个区间,没有重复就向右增大区间,有重复就右移一位。

这样只要记录最大区间就好了。

 

区间范围可记为  [end-max,end)

 

python代码如下…初学,风格不好,见谅。

 

C++版本:

 

还是我C++熟…哈哈哈

 

更新动态规划,DP本质是根据前一状态(位置、下标)的最优解得出当前状态的最优解。

前一状态 和 当前状态 的区别就是: 新增了最后一个元素,它可能和前面的m个元素中的一个冲突,或完全不冲突(不可能同时和两个元素冲突)。

这里m值表示的是前一状态下的最优解max。

当前状态的max = 当前Index – 冲突Index;

这里nDPEnd在不同位置,标识的是不同状态下的max值。

【LeetCode】3. Longest Substring Without Repeating Characters
Tagged on:         
0 0 投票数
Article Rating
订阅评论
提醒

0 评论
内联反馈
查看所有评论