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. Jetty安装配置

    1) 从官方网站下载绿色版的jetty 下载地址:http://download.eclipse.org/jetty/ 我下载的是 jetty-distribution-7.5.4.v20111024 ...

  2. linux下串口工具minicom

    系统环境:ubuntu  14 .04  和ubuntu 16.04 我当时的需要主要是两个,能够看到正常串口输出,并且把串口内容实时输出到文件中 那接下来工作主要是两个:1.安装 2.配置 相信各位 ...

  3. Socket模型(二):完成端口(IOCP)

    为什么要采用Socket模型,而不直接使用Socket? 原因源于recv()方法是堵塞式的,当多个客户端连接服务器时,其中一个socket的recv调用时,会产生堵塞,使其他链接不能继续.这样我们又 ...

  4. SpringMVC整合Mongodb开发,高级操作

    开发环境: 操作系统:windows xpMongodb:2.0.6依 赖 包:Spring3.2.2 + spring-data-mongodb-1.3.0 + Spring-data-1.5 +  ...

  5. Android HttpURLConnection源代码分析

    Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...

  6. FreeSWITCH呼叫参数之sip_cid_type

    这个参数定义了呼叫中主叫信息的头字段类型.支持两种类型: 1. rpidRemote-Party-ID头,这是默认的设置.{sip_cid_type=rpid}sofia/default/user@e ...

  7. apache apr的编译和引用

    各种巧合吧,需要从JAVA转C,经过这一段时间的心理折磨,还是决定先把精力放到C上. 想快速的提高自己,学习相关语言的经典的源码是唯一的“捷径”,从Apache apr开始吧. 一.下载源代码 官网地 ...

  8. [转]同步对象Event的用法

    同步对象Event的用法  首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面.  CreateEvent函数功能描述:创建或打开一个命名的或无名的事 ...

  9. JSTL标签之&lt;c:choose&gt;&lt;c:when&gt;&lt;c:otherwise&gt;标签

    假设是JSTL1.1版本号,使用<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" ...

  10. vim快速指南

    vi编辑器有3种模式:命令模式.输入模式.末行模式.掌握这三种模式十分重要: 命令模式:vi启动后默认进入的是命令模式,任何模式下,按[Esc]键都可以返回命令模式.输入模式:可输入字符,在底部显示“ ...