Longest Substring Without Repeating Characters 解答
Question
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.
Solution
Use extra map to record whether the character has appeared. Remember to consider the situation of last character. Time complexity O(n), space cost O(n).
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() < 1)
return 0;
Map<Character, Integer> map = new HashMap<Character, Integer>();
char[] copy = s.toCharArray();
int length = s.length();
int maxCount = 1;
char start = s.charAt(0);
for (int i = 0; i <= length; i++) {
char tmp;
if (i == length) {
tmp = s.charAt(length - 1);
} else {
tmp = s.charAt(i);
}
if (!map.containsKey(tmp)) {
map.put(tmp, i);
} else {
int startIndex = map.get(start);
int dupIndex = map.get(tmp);
if (startIndex <= dupIndex) {
if (dupIndex < length - 1)
start = s.charAt(dupIndex + 1);
maxCount = Math.max(maxCount, i - startIndex);
}
map.put(tmp, i);
}
}
return maxCount;
}
}
Longest Substring Without Repeating Characters 解答的更多相关文章
- 3.Longest Substring Without Repeating Characters(string; HashTable)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode[3] 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, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
随机推荐
- HTML5 CSS3 诱人的实例 :模仿优酷视频截图功能
一般的视频网站对于用户上传的视频,在用户上传完成后,可以对播放的视频进行截图,然后作为视频的展示图.项目中也可以引入这样的功能给用户一种不错的体验,而不是让用户额外上传一张展示图. 效果图: 看起来还 ...
- 方案:在Eclipse IDE 中搭建Python开发环境
Eclipse是一款功能强大的IDE,Python是一种功能强大的计算机语言,但是Python的IDE环境确实很缺乏,如果在强大的Eclipse中添加Python开发环境,那样就很完美了. 在这里,我 ...
- Objective-C中NSString和NSMutableString的基本用法
int main(int argc, const char * argv[]) { @autoreleasepool { //----------------NSString------------- ...
- python list 按长度分段
def changes(a,b): #list 分段函数,a:数据[(1),(2)]b:长度 for i in xrange(0,len(a),b): yield a[i:i+b] for i in ...
- UITextView 输入长度限制
//还可以输入的长度. - (void)textViewDidChange:(UITextView *)textView { UITextRange *markRange = textView.mar ...
- 01-Java学习笔记
本系列笔记由常彦博整理,请知悉 目 录 一. Java技术基础.................................................................... ...
- 常用JS代码整理
1: function request(paras) { 2: var url = location.href; 3: var paraString = url.substring(url.index ...
- C++中malloc/free和new/delete 的使用
malloc/free 的使用要点 函数malloc的原型如下: void * malloc(size_t size); 用malloc申请一块长度为length的整数类型的内存,程序如下: int ...
- html5前端开发笔记-个人中心
简单的css自适应 PC端 *** 移动端 *** ) *** 一开始的想法就是模仿手机APP 的页面进行布局,首先得有个头部,然后是主题部分,然后加上2个按钮,分别是编辑和退出登录.先布出基本结构. ...
- [MSDN] GROUP BY (Transact-SQL)
来源: https://msdn.microsoft.com/zh-cn/library/ms177673(v=sql.110).aspx 按 SQL Server 中一个或多个列或表达式的值将一组选 ...