Leetcode:Longest Substring Without Repeating Characters 解题报告
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.

SOLUTION 1:
http://blog.csdn.net/imabluefish/article/details/38662827
使用一个start来记录起始的index,判断在hash时 顺便判断一下那个重复的字母是不是在index之后。如果是,把start = map.get(c) + 1即可。并且即时更新
char的最后索引。
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null) {
return 0;
}
int max = 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int len = s.length();
int l = 0;
for (int r = 0; r < len; r++) {
char c = s.charAt(r);
if (map.containsKey(c) && map.get(c) >= l) {
l = map.get(c) + 1;
}
// replace the last index of the character c.
map.put(c, r);
// replace the max value.
max = Math.max(max, r - l + 1);
}
return max;
}
}
SOLUTION 2:
假定所有的字符都是ASCII 码,则我们可以使用数组来替代Map,代码更加简洁。
public int lengthOfLongestSubstring(String s) {
if (s == null) {
return 0;
}
int max = 0;
// suppose there are only ASCII code.
int[] lastIndex = new int[128];
for (int i = 0; i < 128; i++) {
lastIndex[i] = -1;
}
int len = s.length();
int l = 0;
for (int r = 0; r < len; r++) {
char c = s.charAt(r);
if (lastIndex[c] >= l) {
l = lastIndex[c] + 1;
}
// replace the last index of the character c.
lastIndex[c] = r;
// replace the max value.
max = Math.max(max, r - l + 1);
}
return max;
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/LengthOfLongestSubstring.java
Leetcode:Longest Substring Without Repeating Characters 解题报告的更多相关文章
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] 3. 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 最长无重复子串
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 ...
随机推荐
- Mysql模糊查询 select count(*) from sys_invitation where from_id like '%1006%';
select count(*) from sys_invitation where from_id like '%1006%'; 查询结果 select * from sys_invitation w ...
- git学习笔记(四)—— 分支管理
一.创建与合并分支 git branch //查看分支 git branch <name> //创建分支 git checkout <name> //切换分支 git chec ...
- iOS11新特性之LargeTitle
UI风格 在iOS 11中,系统APP使用了这种UI风格.这种风格最明显的变化就是使用了iOS 11的新特性--Large Title和新的SearchController. Demo GitHub: ...
- 第2章 Python基础-字符编码&数据类型 购物车&多级菜单 作业
作业 一.三级菜单 数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, ...
- SQL Server 2008 R2占用内存越来越大两种解决方法
SQL Server 2008 R2运行越久,占用内存会越来越大. 第一种:有了上边的分析结果,解决方法就简单了,定期重启下SQL Server 2008 R2数据库服务即可,使用任务计划定期执行下边 ...
- MySQL的启动与停止
如果MySQL数据库是自己安装的,可以用如下方法分别启动和停止MySQL. 1. MySQL服务器的启动 $mysql_dir/bin/mysqld_safe & (其中&am ...
- [转]Servlet 单例多线程
Servlet如何处理多个请求访问? Servlet容器默认是采用单实例多线程的方式处理多个请求的: 1.当web服务器启动的时候(或客户端发送请求到服务器时),Servlet就被加载并实例化(只存在 ...
- [转]Java中使用Runtime和Process类运行外部程序
帖子1: 使用Runtime.getRuntime().exec()方法可以在java程序里运行外部程序. 1. exec(String command) 2. exec(String comma ...
- Java 8 forEach examples遍历例子
1. forEach and Map 1.1 Normal way to loop a Map. Map<String, Integer> items = new HashMap<& ...
- Windows搭建测试RabbitMq遇到的问题
报错: d:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.5\sbin>rabbitmq-plugins eble rabbitmq_ma ...