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 ...
随机推荐
- JS代码的window.location属性详解
转载:http://www.5icool.org/a/201105/a564.html 如果你稍微懂一些JS代码,一般都会知道 window.location.href 这个属性.并且用该属性获取页面 ...
- C++关于strcpy等函数的安全版本
如下程序: #include <iostream> using namespace std; int main() { ]; strcpy(ch1,"); } 在VS2012上面 ...
- hihoCoder 1041 国庆出游 (DFS)
题意: 小Hi和小Ho准备国庆期间去A国旅游.A国的城际交通比较有特色:它共有n座城市(编号1-n):城市之间恰好有n-1条公路相连,形成一个树形公路网.小Hi计划从A国首都(1号城市)出发,自驾遍历 ...
- SimHash算法
短文本合并重复(去重)的简单有效做法 - 旁观者 - 博客园 短文本合并重复(去重)的简单有效做法 SimHash算法 - ACdreamer - 博客频道 - CSDN.NET SimHash算法
- [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,
在x-code Version 4.3.2 (4E2002)下编译: 在 Controller. m 文件下添加如下实例方法即可: - (void)viewDidUnload { [super vie ...
- JAX-WS 可运行项目
该项目是通过JAX-WS实现的WebService服务,其中包括了1.关于最简单的WebService服务的创建2.关于文件交互的WebService的创建 代码中包括了服务端代码和客户端代码(客户端 ...
- C++: int和string相互转换
假设在一个C++的程序中常常会用到int和string之间的互换.个人建议能够写成一个函数,下次用的时候直接调用就可以. #include <iostream> #include < ...
- Vijos1051. 送给圣诞夜的极光
试题请參见: https://vijos.org/p/1051 题目概述 圣诞老人回到了北极圣诞区, 已经快到12点了. 也就是说极光表演要開始了. 这里的极光不是极地特有的自然极光景象. 而是圣诞老 ...
- linux hash_map
在linux下的hash_map hash_map本身以前本身不属于标准库,是后来引入的.有两种可能:一种可能它被放在了stdext名空间里,那么你就要使用using namespace stdext ...
- Python网络爬虫(6)--爬取淘宝模特图片
经过前面的一些基础学习,我们大致知道了如何爬取并解析一个网页中的信息,这里我们来做一个更有意思的事情,爬取MM图片并保存.网址为https://mm.taobao.com/json/request_t ...