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 ...
随机推荐
- url 中非法字符替换,java 正则替换
url在传输时不允许的一些字符串,参考自:http://www.ietf.org/rfc/rfc1738.txt 以下字符用java正则替换为"_",一句话搞定: "{& ...
- RHEL/CentOS 7.x/6.x/5.x开启EPEL仓库
说明 原文链接 翻译:@adolphlwq 项目地址 这篇指南文章教你如何在 RHEL/CentOS 7.x/6.x/5.x 系统中开启EPEL仓库支持,以便你可以使用 yum 命令 安装额外的标准开 ...
- 上海租房找房建议及条件,上海IT行业开发常见公司的位置地点
上海租房,找房条件 以2号地铁线为中心,优先选择(回家方便,重点!),交通设施较集中地铁:2,3,4 区:普陀区,静安区,长宁区,闸北区,浦东新区,闵行区,徐汇区 路:镇坪路,威宁路,娄山关路,中山公 ...
- YAML中重复的KEY的判断
package com.test.util; import java.io.BufferedReader; import java.io.FileInputStream; import java.io ...
- 一张图告诉你:Android系统哪代强?
一张图告诉你:Android系统哪代强? 新浪科技 王上 谷歌发布Android 1.5 的时候,开始以甜点命名,作为每个版本代表的甜点的尺寸越变越大.谷歌在2014年10月中旬发布了Android ...
- OAuth2.0实战之微信授权篇
微信开发三大坑: 微信OAuth2.0授权 微信jssdk签名 微信支付签名 本篇先搞定微信OAuth2.0授权吧! 以简书的登陆页面为例,来了解一下oauth2.0验证授权的一些背景知识: 1) 传 ...
- 代码管理(四)SVN和Git对比
在日常运维工作中,经常会用到版本控制系统,目前用到最广泛的版本控制器就是SVN和Git,那么这两者之间有什么不同之处呢?SVN(Subversion)是集中式管理的版本控制器,而Git是分布式管理的版 ...
- /proc 目录详细说明
/proc路径详细: Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...
- explicit_defaults_for_timestamp参数
Introduced 5.6.6 Deprecated 5.6.6 Command-Line Format --explicit_defaults_for_timestamp=# System Var ...
- python类内init外声明的属性与init内声明的对象属性的访问和操作区别
python类内init外声明的属性与init内声明的对象属性的访问和操作区别(面试题) 1.在ipython中输入以下代码,其输出会是什么? In [1]: class ClassOut: ...: ...