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 ...
随机推荐
- hdu2059 龟兔赛跑
hdu2059 龟兔赛跑 动态规划 题目描述: Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州 ...
- 雅虎工程师初始化css
/*css reset code */ /**** 文字大小初始化,使1em=10px *****/ body { font-size:62.5%; } /* for IE/Win */ html&g ...
- python - 文件
''' 模式 描述 r 以读方式打开文件,可读取文件信息. w 以写方式打开文件,可向文件写入信息.如文件存在,则清空该文件,再写入新内容 a 以追加模式打开文件(即一打开文件,文件指针自动移到文件末 ...
- PHP性能优化学习笔记--语言级性能优化--来自慕课网Pangee http://www.imooc.com/learn/205
使用ab进行压力测试 ab -n行数 -c并发数 url 重点关注下面两点: 1.Request per secend : 每秒可接收的请求数 2.Time per request : 每次请求所耗费 ...
- (3)选择元素——(5)为项目列表加样式(Styling list-item levels)
Let's suppose that we want the top-level items, and only the top-level items, to be arranged horizon ...
- LR脚本自定义显示Controller虚拟用户状态
在场景监控的过程中,想知道场景运行时Vusers的运行状态以及每一个Vuser虚拟用户在本次场景运行的过程共迭代了多少次,那么就需要在VuGen脚本中自定义显示虚拟用户状态信息. 代码如下: stat ...
- date命令小结
date命令是查看日期时间的常用命令,date MMDDhhmmYY.ss(修改顺序)用来更改时间 linux时间分为系统时间和硬件时间, [root@www doc]# clock--------- ...
- Chrome 开发者工具详解(4):Profiles 面板
概述 当前使用的Chrome最新版为54.0.2840.71,这个版本的Profiles面板比之前提供的功能更多也更强大,下面是该面板所包含的功能点: Record JavaScript CPU Pr ...
- 用Apache Ivy实现项目里的依赖管理
Apache Ivy是一个管理项目依赖的工具. 它与Maven Apache Maven 构建管理和项目管理工具已经吸引了 Java 开发人员的注意.Maven 引入了 JAR 文件公共存储库的概念 ...
- 查文件大小列表 MySQL问题
du -sh /* | sort -nr 打开网站发现Too many connections The server quit without updating PID file (/usr/loca ...