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 解题报告的更多相关文章

  1. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  2. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  3. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  4. [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  8. C++ leetcode Longest Substring Without Repeating Characters

    要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...

  9. [LeetCode]Longest Substring Without Repeating Characters题解

    Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...

随机推荐

  1. MATLAB R2018a 安装教程

    1.下载相应的 MATLAB  R2018a  版本如下:[matlab2018的百度云盘:链接:https://pan.baidu.com/s/1OV242y6EV6auvG3DvvqD8A 密码: ...

  2. CMFCPropertyGridProperty用法

    MFCPropertyGridCtrl 是VC 2008 pack中的控件类. CMFCPropertyGridProperty这个控件类中的属性值类类. 针对修改属性后,对属性值改变的消息处理: 方 ...

  3. React(0.13) 定义一个动态的组件

    1.因为jsx将两个花括号之间的内容渲染为动态值,只需要引用对应的变量即可 <!DOCTYPE html> <html> <head> <title>R ...

  4. Charles 网络抓包工具

    1.Charles 简介 Charles 是在 Mac.Linux 或 Windows 下常用的 http 协议网络包截取工具,在平常的测试与调式过程中,掌握此工具就基本可以不用其他抓包工具了.Cha ...

  5. 【Linux】双向重导向命令tee

    想个简单的东西,我们知道 > 会将数据流整个传送给文件或装置,因此我们除非去读取该文件或装置, 否则就无法继续利用这个数据流.万一我想要将这个数据流的处理过程中将某段信息存下来,应该怎么做? 利 ...

  6. no OPENSSL_Applink错误的解决方法

    原文链接: http://www.cnblogs.com/sdnyzhl/archive/2012/12/11/2813210.html 自己按照openssl中介绍的编译,安装openssl,其间编 ...

  7. 跟我学SharePoint 2013视频培训课程——自定义网站导航(4)

    课程简介 第4天,自定义SharePoint 网站导航 视频 SharePoint 2013 交流群 41032413

  8. 【转载】linux 测试机器端口连通性方法

    转载原文:http://blog.csdn.net/z1134145881/article/details/54706711 下面一一介绍: 1 telnet方法 2 wget方法 3 ssh方法 4 ...

  9. 正则和xpath在网页中匹配字段的效率比较

    1. 测试页面是  https://www.hao123.com/,这个是百度的导航 2. 为了避免网络请求带来的差异,我们把网页下载下来,命名为html,不粘贴其代码. 3.测试办法: 我们在页面中 ...

  10. mac 下python使用venv 虚拟环境

    1.安装virtualenv :pip3 install virtualenv 2.创建虚拟环境命令:virtualenv --no-site-packages venv 在当前目录创建一个虚拟环境v ...