[LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without
repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
涉及longest substring的题目一般都有着明显的DP特征。这题的一种一维DP思路是:将全部不反复的子字符串按结尾的数组下标分类,让maxEndsWith[i]表示字符串的结尾以i为数组下标,并取最长长度。
对于一个给定的不存在反复字符的字符串。以及一个试图在尾部新添的字符:
1)假设新添字符并不跟所给字符串里的字符反复,那么最长长度加1,即maxEndsWith[i] = maxEnds[i - 1] + 1。
2)假设新加入反复,那么加入此字符的代价是要删除之前的反复字符。以及不计数该反复字符之前的字符。
这样的思路事实上看起来有些类似于数据流算法,核心是维护一个sliding window,保证这个window里全部元素都不反复。涉及去重操作。使用set是最好只是啦。只是这里的难度在于发现反复字符后。不光删除改反复字符,还要删除在此之前的全部字符。
对于满足删除操作,能够用一种非常"别扭"的实现方式。即不用set而改用map,key存字符。value存index。然后另外把map里涵盖的全部index存在一个queue(或者说deque)里。
这样一来。插入和删除都同一时候须要改动map和queue。
事实上对于这样的DP,还能够有一种更简洁优雅的实现方式,就是利用LinkedHashSet这个数据结构。
不同于HashSet。LinkedHashSet会依据元素插入的顺序,把各个元素串联起来成一个链表,所以在遍历的时候会严格遵循插入顺序。由此观之,使用LinkedHashSet的优点非常明显。因为我们这里维护的是一个sliding
window,在反复字符位置之前的字符肯定都是在之前插入window的,插入顺序和遍历顺序都排在反复字符的前面。因此,一旦遇到反复字符。就能够从sliding
window的开头開始删除,一直按遍历顺序删到反复字符出现。并一起删掉。
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0)
return 0;
int ret = 1;
Set<Character> set = new LinkedHashSet<Character>();
int[] maxEndsWith = new int[s.length()];
maxEndsWith[0] = 1;
set.add(s.charAt(0));
for (int i = 1; i < s.length(); ++i) {
char c = s.charAt(i);
if (!set.contains(c)) {
set.add(c);
maxEndsWith[i] = maxEndsWith[i - 1] + 1;
} else {
Iterator<Character> it = set.iterator();
while (it.hasNext()) {
char front = it.next();
it.remove();
if (front == c) {
break;
}
}
set.add(c);
maxEndsWith[i] = set.size();
}
ret = Math.max(maxEndsWith[i], ret);
}
return ret;
}
遍历顺序删到反复字符出现。并一起删掉。
注意以上代码尽管有2层循环。可是均摊的时间复杂度还是O(N)。由于每一个字符都只会被增加的window一次。而且只会从window中删除一次。另外这里对数据总共就唯独一次扫描。典型的数据流算法特点。
[LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...
- [LeetCode]Longest Substring Without Repeating Characters题解
Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode——Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [Leetcode] Longest Substring Without Repeating Characters (C++)
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
随机推荐
- R与数据分析旧笔记(十)非线性模型
非线性模型 非线性模型 例子:销售额x与流通费率y > x=c(1.5,2.8,4.5,7.5,10.5,13.5,15.1,16.5,19.5,22.5,24.5,26.5)> y=c( ...
- ThinkPHP第二十二天(表单令牌、相对路径、扩展配置载入、$Think获取系统变量、$_SERVER('HTTP_REFERER')前页地址)
1.表单令牌开启配置 'TOKEN_ON'=>true 2.相对路径:在thinkphp中,存在单入口index.php,所以程序中的根目录都是以index.php所在的文件夹为根目录,故用./ ...
- python 程序中设置环境变量
python 中调用系统命令有三种方法: 1.os.system('command') ,这个方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 ...
- PullToRrefresh自定义下拉刷新动画
首先,下载著名的刷新框架https://github.com/chrisbanes/Android-PullToRefresh,其中simple为demo,library和extras作为项目包导入到 ...
- JavaEE Tutorials (12) - 创建和使用基于字符串的Criteria查询
12.1基于字符串的Criteria API查询概述17812.2创建基于字符串的查询17812.3执行基于字符串的查询179
- ubuntu安装XHProf
1. 安装XHProf wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2 s ...
- 编写一个程序, 将 a.txt 文件中的单词与 b.txt 文件中的 单词交替合并到 c.txt 文件中, a.txt 文件中的单词用回车符 分隔, b.txt 文件中用回车或空格进行分隔。
package cn.itcast; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public ...
- The Painter's Partition Problem Part II
(http://leetcode.com/2011/04/the-painters-partition-problem-part-ii.html) This is Part II of the art ...
- preg_replace的用法
<?php $str1 = "03/28/2015"; // 要替换成 2015-03-28 echo preg_replace("/([0-1][1-9])\/( ...
- 5.7.1.2 eval() 方法
现在我们介绍最后一个方法,这大概是ECMAScript语言中最强大的一个方法:eval().eval()方法就想一个完整的ECMAScript解析器,它只接受一个参数,即要执行的ECMAScript( ...