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 ...
随机推荐
- CentOS下安装无线网卡驱动 (转)
1. 确定自己的网卡和内核版本:lspci | grep Network #根据输出的信息确定网卡的型号.uname -a #确定内核版本 2. 配置yum使用RPMForg ...
- UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>
L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- 【HDU1754】I Hate It(线段树)
update:单点替换 query:区间最值 #include <iostream> #include <cstring> #include <cstdlib> # ...
- Unity 脚本函数生命周期
Awake(),一般我们在这里做一些组件的获得,比如使用getcomponent方法. Start(),我们可以在这里给变量赋值. FixUpdate(),固定更新,因为这里得更新速度为固定(可以在T ...
- c#图像处理入门
一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetP ...
- MVC Razor视图引擎
Razor 不是编程语言.它是服务器端标记语言. Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法 当网页被写入浏览器时,基于服务器的代码能够创建动 ...
- AOP的实现原理——动态代理
IOC负责将对象动态的 注入到容器,从而达到一种需要谁就注入谁,什么时候需要就什么时候注入的效果,可谓是招之则来,挥之则去.想想都觉得爽,如果现实生活中也有这本事那就爽 歪歪了,至于有多爽,各位自己脑 ...
- sprig——jar包
Struts. Hibernate.Spring这类的框架给我们开发带来非常大的好处,让我们更加快速.有效的开发.所以我们在开发中通常都会用到各种框架,每个框架 都有很多jar包,每个jar都有各自不 ...
- Android加载图片OOM错误解决方式
前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...
- BOOST 线程完全攻略 - 扩展 - 线程消息通讯
// controlled_module_ex.hpp : controlled_module类的扩展 // 增强线程之间消息通讯 // 增加线程安全启动和安全关闭功能 // 增加定时器功能 #p ...