LeetCode——Longest Substring Without Repeating Characters
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.
问题描述:
给定一个字符串,寻找最长无重复子串,返回最长长度。
解决方案:
1、暴力搜索,O(n^2)不考虑。
2、举个例子,abcdecf
说明:使用两个游标,i、j 初始为0,len为字符串长度,maxLen为要返回的无重复最长子串的长度,exist数组为标识元素是否重复,默认支持ASCII,数组大小开到256.
a)对于字符串 a b c d e c f 来说:i 增长到第二个'c'的下标[5],发现之前'c'出现过;
b)这时,首先更新最大长度;
c)然后while循环执行 j++ 自增到第一个'c',同时将第一个'c'之前的元素的存在标志清除。
d)i++,j++,从a b c d e c f 处继续向后执行
e)最后在return之前,再更新maxLen
public int lengthOfLongestSubstring(String s) {
int len = s.length();
boolean[] exist = new boolean[256];
for (int i = 0; i < 256; i++) {
exist[i] = false;
}
int i = 0, j = 0;
int maxLen = 0;
while (i < len) {
if (!exist[s.charAt(i)]) {//如果访问的元素没有出现过
exist[s.charAt(i)] = true;
i++;
} else {//发现两个一样的,现在i指向两个元素中的第二个 maxLen = Math.max(i - j, maxLen);//更新最大长度
while (s.charAt(i) != s.charAt(j)) {
exist[s.charAt(j)] = false;//重置exist数组
j++;
}//while循环结束后,现在i、j都是指向那个重复元素,j指向第一个
i++;
j++;
}
}
maxLen = Math.max(maxLen, len - j);//最后再更新最大程度
//System.out.println(maxLen);
return maxLen;
}
重新做了一遍,发现还没第一次做的好:
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
HashMap<Character, Integer> exist = new HashMap<>();
int len = 0, res = 0, last_j = 0;
for (int i = 0; i < s.length(); i++) {
if (exist.get(s.charAt(i)) == null) {
exist.put(s.charAt(i), i);
len++;
} else {
int j = exist.get(s.charAt(i));
len = i - j;
for (int k = last_j; k <= j; k++) {
exist.remove(s.charAt(k));
}
exist.put(s.charAt(i), i);
last_j = j + 1;
}
res = Math.max(len, res);
}
System.out.println(res);
return res;
}
原创文章,转载请注明出处。
LeetCode——Longest Substring Without Repeating Characters的更多相关文章
- [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 (C++)
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- Qt知识点、疑难杂症的治疗
Q: QVariant 保存指针数据 A1: 1,使用QVariant::fromValue((void*)target)保存数据 2,使用(ShortcutItem*)(v.value<v ...
- JDK5-枚举
1. 使用普通类模拟枚举 public class Weekday { private Weekday() {} // 私有化 public static final Weekday MONDAY = ...
- Android群英传》读书笔记 (1) 第一章 Android体系与系统架构 + 第二章 Android开发工具新接触
第一章 Android体系与系统架构 1.Dalvik 和 ARTDalvik好比是一辆可折叠的自行车,平时是折叠的,只有骑的时候,才需要组装起来用.ART好比是一辆组装好了的自行车,装好就可以骑了. ...
- Oracle 存储过程(2)
http://www.cnblogs.com/chinafine/archive/2010/07/12/1776102.html http://blog.itpub.net/29485627/view ...
- 下载文档时Safari浏览器下载后出现".html"问题
下载代码是需要设置 Response.ContentType = "application/octet-stream", 不要设为application/x-msdownload, ...
- Wpf控件ListBox使用实例2
2.Xaml绑定选择结果 <StackPanel Orientation="Vertical"> <TextBlock Margin="10,10,10 ...
- Web字体工具整理,网页图标字体以及使用方法整理
常用网站app logo 下载: http://www.iconfont.cn/collections/show/268?spm=a313x.7781069.0.0.nDYawz 1.Web字体免费生 ...
- (转)一步一步学习PHP(4)——函数
相信每个人在学习PHP之前至少都有着一定的C语言,或者是C++/Java/C#等其他语言的基础,所以在这里也不从头开始说起,只是来谈谈PHP方法的独特之处. 1. 解决作用域问题 在上一节谈到了PHP ...
- 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...
- iOS 上传项目常见问题
一. Archive项目 时,出现"Your build settings specify a provisioning profile with the UUID "XXX&qu ...