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 ...
随机推荐
- 编程实践笔记{Java 线程 并发处理 Webservice}(转)
http://www.cnblogs.com/mingzi/archive/2009/03/09/1406694.html 1, 保证线程安全的三种方法: a, 不要跨线程访问共享变量 b, 使共享变 ...
- 蛋疼的经历--wireshark不能启动的问题
事情是这样子的,最近新入职,安装了wireshark,,,在急需要其观察数据包结构,,,,写代码时,,,,卡了,,,我的天!!! 刚开始是提示说,找不到动态链接库api-ms-win-crt-runt ...
- kubernetes架构之二
一.概述 IaaS:即基础设施即服务,通过虚拟化和分布式存储等技术,实现对包括服务器.存储设备.网络设备等各种物理资源的抽象:从而形成了一个可扩展.可按需分配的虚拟资源池.最具代表性的IaaS产品有A ...
- 【Android】Android连接SQLite3数据库的操作
在前面使用SQLite3的时候,并没有留意到有SQLiteOpenHelper这个类,所以只好在Activity里面去创建和维护数据库跟数据表的创建. 但是,现在有了SQLiteOpenHelper这 ...
- 【Spring】Spring之向 IOC 容器注入对象的三种方式
关于Spring的搭建可参见:浅析Spring框架的搭建.在测试之前还是应该先将环境配置好,将相关Jar包导进来.Spring创建的对象,默认情况下都是单例模式,除非通过scope指定. 向IOC容器 ...
- c# 中内部类的简单介绍 C#内部类
最近在看java一方面的书籍,看到一个很奇怪的问题,java类中还可以再定义一个类,这种结构非常特殊!后来才发现我知识浅薄了,原来C#中也有内部类,之前都一直没有注意过这个语法结构! 使用内部类有这样 ...
- git detached
git提交的时候,本地已经提交,却怎么也推送不到服务器,也没显示错误,只显示 everything-up-to-date : 原因是git不在master分支,而是处于detached head(匿名 ...
- TextView中显示链接 定义颜色
<TextView android:id="@+id/textView" android:layout_width="match_parent" andr ...
- 关于ps cs5的一些问题
一.photoshop cs5 默认在窗口中浮动方法 1.打开“编辑>首选项>界面”在“面板和文档”里把“以选项卡方式打开图像”的勾选去掉 2.点击菜单栏“窗口>排列>使所有内 ...
- linux 监控性能学习笔记(1)
top命令中的 load average后面的三个数字分别表示距离现在一分钟,五分钟,十五分钟的负载情况. 在单核系统中100%利用负载标识为1.00,双核系统标识2.00 四核系统标识为4.00 因 ...