Longest Substring Without Repeating Characters leetcode java
题目:
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.
题解:
这道题下面的讲解和代码完全引用http://blog.csdn.net/linhuanmars/article/details/19949159
“原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
这
道题用的方法是在LeetCode中很常用的方法,对于字符串的题目非常有用。 首先brute force的时间复杂度是O(n^3),
对每个substring都看看是不是有重复的字符,找出其中最长的,复杂度非常高。优化一些的思路是稍微动态规划一下,每次定一个起点,然后从起点走到
有重复字符位置,过程用一个HashSet维护当前字符集,认为是constant操作,这样算法要进行两层循环,复杂度是O(n^2)。
最
后,我们介绍一种线性的算法,也是这类题目最常见的方法。基本思路是维护一个窗口,每次关注窗口中的字符串,在每次判断中,左窗口和右窗口选择其一向前移
动。同样是维护一个HashSet,
正常情况下移动右窗口,如果没有出现重复则继续移动右窗口,如果发现重复字符,则说明当前窗口中的串已经不满足要求,继续移动有窗口不可能得到更好的结
果,此时移动左窗口,直到不再有重复字符为止,中间跳过的这些串中不会有更好的结果,因为他们不是重复就是更短。因为左窗口和右窗口都只向前,所以两个窗
口都对每个元素访问不超过一遍,因此时间复杂度为O(2*n)=O(n),是线性算法。空间复杂度为HashSet的size,也是O(n).
”
代码如下:
1 public int lengthOfLongestSubstring(String s) {
2 if(s==null || s.length()==0)
3 return 0;
4
5 HashSet<Character> set = new HashSet<Character>();
6 int max = 0;
7 int walker = 0;
8 int runner = 0;
9 while(runner<s.length()){
if(set.contains(s.charAt(runner))){
if(max<runner-walker)
max = runner-walker;
while(s.charAt(walker)!=s.charAt(runner)){
set.remove(s.charAt(walker));
walker++;
}
walker++;
}else
set.add(s.charAt(runner));
runner++;
}
max = Math.max(max,runner-walker);
return max;
}
Longest Substring Without Repeating Characters leetcode java的更多相关文章
- Longest Substring Without Repeating Characters ---- LeetCode 003
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters -- LeetCode
原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 这道题用的方法是在LeetC ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Java [leetcode 3] Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...
- [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: ...
随机推荐
- web项目使用配置web.xml实现重定向
1.实现方式 使用servlet和servlet-mapping实现重定向 <!-- vip登录页面做特殊处理 --> <servlet> <servlet-name&g ...
- poj1273(Edmonds-Karp)
这道题可以算是例题了. 求解最大流,采用EK算法,用广搜查找增广路径,找到后更新网络流矩阵,循环执行直至找不到增广路径为止.这里要小心的是重复边的情况. 程序也是参照了网上的模版来写的,有一些技巧.如 ...
- vsftp 虚拟用户+MySQL认证独立家目录
centos7 系统 安装包 yum -y install mariadb vsftpd openssl-devel mysql-devel pam-devel yum -y groupinsta ...
- Linux嵌入式文件系统(网络文件系统)
<文件系统定义> 怎么将文件和文件目录加载到linux内核中,这一种加载的方式就叫做文件系统 <建立根文件系统目录和文件> <创建目录> 1)在linux系统中使用 ...
- android viewHolder static 静态
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 不是静态内部类 会 持有 外部类的 引用. 就像经常自定义的 适配器 类 作为内部类 ...
- QT学习笔记7:C++函数默认参数
C++中允许为函数提供默认参数,又名缺省参数. 使用默认参数时的注意事项: ① 有函数声明(原型)时,默认参数可以放在函数声明或者定义中,但只能放在二者之一.建议放在函数声明中. double sqr ...
- [POI2013]Bajtokomputer
[POI2013]Bajtokomputer 题目大意: 给定一个长度为\(n(n\le10^6)\)的由\(\{-1,0,1\}\)组成的序列,你可以进行\(A_i+=A_{i-1}\)这样的操作, ...
- Educational Codeforces Round 13 A. Johny Likes Numbers 水题
A. Johny Likes Numbers 题目连接: http://www.codeforces.com/contest/678/problem/A Description Johny likes ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- Using PWM Output as a Digital-to-Analog Converter
http://www.ti.com/lit/an/spraa88a/spraa88a.pdf http://www.ti.com/litv/zip/spraa88a The high-resoluti ...