[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 ...
随机推荐
- nginx install lua module
#install luajit #http://luajit.org/download.html .tar.gz cd LuaJIT- make install PREFIX=/home/allen. ...
- C++学习之使用new的注意事项
C++学习之使用new的注意事项 在构造函数中使用new来初始化对象的指针成员成员时必须特别小心,具体的说,应该如下这样做: 一.如果在构造函数中使用new来初始化指针成员,则应该在析构函 ...
- Kafka主要参数详解
原文档地址:http://kafka.apache.org/documentation.html https://kafka.apache.org/08/documentation#configur ...
- Models——英语学习小技巧之四
Models are very important, here model means role model, is kind of like a hero. It's someone that ...
- php以fastCGI的方式运行在iis下,遇到的文件系统权限问题及解决方法
今天准备将一个php demo放在IIS下运行,网站在IIS下的配置是这样的: 应用程序池是集成模式下的.net framework 2.0(2.0或4.0没什么关系,因为php以fastCGI的方式 ...
- sqlplus命令手册
show errorshow allshow usersqlplus show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看所有系统变量值 SQ ...
- 导出csv文件代码示例
//当数据量达到一定级别后(大概60000以上),excel会溢出,不能全部显示,而新版的csv好像不会出现这个问题.为什么用好像,我也是听别人说,暂时没有去验证. <?php $sql = & ...
- POJ2151-Check the difficulty of problems(概率DP)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4512 ...
- MVC Razor中 如何截断字符串
有时候显示的内容过长,使用MVC编程时,如何截断显示的内容呢.我知道你肯定有很多办法这样做的,但是在学习MVC时,还是使用一些新的办法做吧> Razor 标记语法编程. @helper Trun ...
- oracle 计算两个时间之间的月份差,相差几个星期,相差多少天
相差多少天: sysdate-to_date('1991-01-01','YYYY-MM-DD'))<7 and (sysdate-to_date('1991=01=01','YYYY-MM ...