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 ...
随机推荐
- ThinkPHP+jQuery EasyUI Datagrid查询数据的简单处理
ThinkPHP和jQuery EasyUI这两个都是不错的框架,现在要把它两个整合到一块,做个简单的Ajax调用查询. 在ThinkPHP模板中引入EasyUI的相关文件,然后设置按钮3的调用: & ...
- 如何写UI及屏幕适配的一些技巧
总结一下关于UI布局及屏幕适配的一些实战技巧,尤其使用纯代码,会对提升效率及代码易于维护等方面有明显帮助,这里提到的没有使用任何Xib, 如果不是在外包公司,也推荐大家多使用甚至完全使用纯代码布局UI ...
- excel中的数据粘贴不全到plsql中,excel 粘贴后空白,Excel复制粘贴内容不全
http://zhidao.baidu.com/link?url=pHZQvfWJzI-lQjl4uP86q4GLcpYHu4o-fdjiYegJS0Cy5HEq5oz0YrUye3iHjmv5CJ3 ...
- 使用Cordova搭建Andoid和iOS开发环境
最近在了解cordova ,下面的分享出来 大家可以看看, 我 有空也按照这个写写demo 1.下载node.js,进行安装 https://nodejs.org/en/ 2.安装cordova ...
- atom介绍
在公司微信群,看到activate-power-mode插件的效果,很绚丽,才知道github自己出了一个自己的编辑器atom 官网地址 https://atom.io 官网看了下,atom编辑器的特 ...
- 31天重构学习笔记(java版本)
准备下周分享会的内容,无意间看到.net版本的重构31天,花了两个小时看了下,可以看成是Martin Fowler<重构>的精简版 原文地址:http://www.lostechies.c ...
- 更改jdk后,eclipse运行jsp出错
1.错误: 在Eclipse下启动tomcat的时候,报错为:Eclipse下启动tomcat报错:The archive: C:/Program Files(x86)/Java/jdk1.7.0_1 ...
- Oracle 12C -- 扩展varchar2、nvarchar2、和raw数据类型的大小限制
在12C中,varchar2,nvarchar2和raw类型从之前的4K扩展到32K 升级到12C后,参数max_string_size默认值是standard,即不改变varchar2.nvarch ...
- oracle数据库rman异地恢复
自己想做两组rac之间的data guard,由于datafile,controlfile,甚至是archivelog都是存放在asm上的,直接复制数据有点不现实,asm磁盘总归都是要用的,所以想从a ...
- Android Developers:从一个Activity获取结果
启动其它Activity不是单向的.你也能启动其它Activity并获取一个返回结果.为了获取一个结果,调用startActivityForResult()方法(替代startActivity()方法 ...