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: ...
随机推荐
- odoo发送信息到微信公众平台、企业微信
目录 odoo发送信息到微信 @(odoo client.message.send_text) odoo发送信息到微信 在odoo平台中进行项目开发的时候有时会用到跟其他平台对接发送信息. 这里我写一 ...
- matlab绘制心形函数
matlab 7.0 绘制二维.三维心形函数 又到周六,下周就要迎来春节小长假了,想想都有些激动.在外漂了一整年,总于可以回家和父母团聚了,还有吃好吃的...,哎呀~想想都流口水呢.不过先 ...
- 获取Android apk的包名
Read the package name of an Android APK aapt dump badging <path-to-apk> | grep package:\ name
- (Nginx) URL REWRITE
URL重写的基础介绍 把URI地址用作参数传递:URL REWRITE 最简单的是基于各种WEB服务器中的URL重写转向(Rewrite)模块的URL转换: 这样几乎可以不修改程序的实现将 news. ...
- Gunicorn配置部分的翻译
写在前面,虽然翻译得很烂,但也是我的劳动成果,转载请注明出处,谢谢. Gunicorn版本号19.7.1 Gunicorn配置 概述 三种配置方式 优先级如下,越后的优先级越大 1.框架的设置(现在只 ...
- BZOJ 4419: [Shoi2013]发微博 set模拟
4419: [Shoi2013]发微博 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4419 Description 刚开通的SH微博共 ...
- GIT(3)----问题汇总
1.git pull出现的合并问题: Please enter a commit message to explain why this merge is necessary,especially i ...
- bash编程之 ~制作Mini Linux系统~
说明1:在一个Linux宿主机系统上,通过以上步骤,可以制作一个微小的Linux系统(可以放置在U盘中等),然后在其它的主机(虚拟机或者物理机)上运行,以实现最小化定制系统的目的. 说明2:上图中黑色 ...
- 使用Chrome快速实现数据的抓取(一)——概述
对于一些简单的网页,我们可以非常容易的通过Develop Tool来获取其请求报文规律,并仿照其构建报文来获取页面信息.但是,随着网页越来越复杂,许多页面是由js动态渲染生成的.要获取这类信息,则需要 ...
- java基础学习总结——网络编程
一.网络基础概念 首先理清一个概念:网络编程 != 网站编程,网络编程现在一般称为TCP/IP编程.