[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 ...
随机推荐
- Spring总结——控制反转,注入(配置和注解两种方式)
一.Spring的容器: 1.什么是控制反转:传统的方法,当某个java对象A需要调用对象B时,是由调用者(对象A)通过new关键字来创建对象B的(也可以说类A依赖类B),而在Spring中,则是由s ...
- 1)③爬取网易It方面部分新闻
__author__ = 'minmin' #coding:utf-8 import re,urllib,sgmllib,os #根据当前的url获取html def getHtml(url): pa ...
- The error indicates that IIS is in 32 bit mode, while this application is a 64 b it application and thus not compatible.
I was trying to install a new WSS v3 Sharepoint on a 64 bit Windows 2003 server today but the instal ...
- Delphi2010的RTTI增强
Delphi编译的文件体积增大了很多.很大一部分原因是因为Delphi2010默认提供了全信息的RTTI. 每一个数据类型都有全部运行时信息.例如可以在运行时获得结构体的成员以及成员类型等. 这个功能 ...
- cocos2dx tag和zorder
当一个渲染对象加入到两外一个渲染对象中时,可以有两个可选参数,一个时tag,一个是order virtual void addChild(CCNode * child); virtual void a ...
- oracle db_unnqiue_name db_name sid_name instance_name service_name
- 使用java API查询java类
一.java API的下载地址 前面列举了常用的java类,但只是介绍了功能,具体详细的用法(比如要知道该类的属性和方法)要需要调用java的API(Application Program Inter ...
- 30天自制操作系统第八天学习笔记(u盘软盘双启动版本)
暑假学习小日本的那本书:30天自制操作系统 qq交流群:122358078 ,更多学习中的问题.资料,群里分享 environment:开发环境:ubuntu 第八天的学习思考: 关于鼠标是怎么 ...
- CXF详细介绍
首先介绍下CXF的拦截器:简单地说,CXF使用流水线型(或者说总线型)处理机制,它的核心是一个Bus.一个客户端的请求或者一个对客户端桩代码的调用被组织成为一个Message.同时,所有的CXF功能都 ...
- Mybatis学习之JDBC缺陷
1.JDBC存在的问题 1.将sql语句硬编码到java代码中,如果修改sql语句,需要修改java代码,重新编译.系统可维护性不高. 设想如何解决?(将sql单独 配置在配置文件中) 2.数据库连接 ...