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: ...
随机推荐
- 【知了堂学习笔记】java web 简单的登录
最近皮皮潇在学习java web,刚接触了简单的东西,所以今天给大家带来一个简单的登录实现. 页面: 页面代码: <%@ page language="java" conte ...
- Oracle数据库DDL,DML,视图,PLSQL编程
动手敲~~~ --创建一个表空间--beijing create tablespace beijing datafile 'c:\beijing.dbf' size 100m autoextend o ...
- mongodb cmd 常用命令
如题,命令如下: 1.连接远程数据库命令 mongo -u admin -p admin 192.168.0.197:27017/test 2.查看当前版本 db.version(); 3.mongo ...
- Opencv学习笔记5:Opencv处理彩虹图、铜色图、灰度反转图
一.概述: 人类能够观察到的光的波长范围是有限的,并且人类视觉有一个特点,只能分辨出二十几种灰度,也就是说即使采集到的灰度图像分辨率超级高,有上百个灰度级,但是很遗憾,人们只能看出二十几个,也就是说信 ...
- 命令神器:lsof 常用
lsof -i 显示所有网络连接lsof -i 6 获取IPv6信息lsof -itcp 显示tcp连接lsof -i:80 显示指定端口信息lsof -i@172.12.5.6 显示指定ip连接ls ...
- Git 初学者使用指南及Git 资源整理
Git 资源整理 Git is a free and open source distributed version control system designed to handle everyth ...
- classList
1,classList.remove(value) // 删除value项 2, classList.contains(value) // 判断列表中是否存在给定的值,存在返回true,否则返回fal ...
- BZOJ 4554: [Tjoi2016&Heoi2016]游戏 二分图匹配
4554: [Tjoi2016&Heoi2016]游戏 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4554 Descripti ...
- bio、nio、aio及select、poll、epoll
BIO与NIO.AIO的区别http://blog.csdn.net/skiof007/article/details/52873421 select.poll.epoll之间的区别总结http:// ...
- 在C#中实现视频播放器
当我们需要在C#中实现视频播放器的时候,可以使用如下几种方法: 一.使用MediaPlayer ActiveX控件 在C#中支持视屏播放器最简单的方式就是插入MediaPlayer控件了,在WPF中还 ...