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. 命令行参数解析函数getopt和getopt_long函数【转】

    原文地址:http://blog.csdn.net/cashey1991/article/details/7942809 getopt和getopt_long函数   平时在写程序时常常需要对命令行参 ...

  2. Linux shell命令:用 !$ 防止误操作

    shell 的通配符匹配非常强大,不过也非常危险,不少同学都碰到这样的问题,比如 rm a*,结果一个手抖,a 和星号之间多了个空格,结果目录下的文件都灰飞烟灭了…… bash 支持一个特殊的变量 ! ...

  3. 树莓派进阶之路 (024) - windows远程桌面连接树莓派通过xrdp服务(转)

    本文转载:http://www.cnblogs.com/edgexie/p/6527992.html 在网上看到很多关于windows远程桌面连接树莓派的教程.我也按照教程试过了,遇到了几个坑.特意记 ...

  4. jQuery 自定义网页滚动条样式插件 mCustomScrollbar 的介绍和使用方法(转)

    系统默认的滚动条样式,真的已经看的够恶心了.试想一下,如果在一个很有特色和创意的网页中,出现了一根系统中默认的滚动条样式,会有多么的别扭. 为了自己定义网页中的滚动条的方法,我真的已经找了很久了,就目 ...

  5. 利用hadoop来解决“共同好友”的问题

    假设A有好友B C D:B有好友A C D E:C有好友A B D E:D有好友A B C E;E有好友B C D. A -> B C D B -> A C D E C -> A B ...

  6. SQL数据缓存依赖总结

    以前只听过SQL server数据缓存依赖,但一直没使用,由于项目需要,才研究了一番,发现了一个很诡异的问题,竟然是一个操作顺序问题导致的. SQL server数据缓存依赖有两种实现模式,轮询模式, ...

  7. 代码管理(二)sourcetree 安装与使用

    一 .SourceTree简介 SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,拥有可视化界面,容易上手操作.同时它也是Mercurial和Subve ...

  8. Android基础之——CountDownTimer类,轻松实现倒计时功能

    在发现这个类之前,一直是用的handler,子线程发消息,UI线程进行倒计时的显示工作.前几天在做一个倒计时显示的时候发现了这个类,用起来非常方便 翻看了下源代码.内部已经帮我们实现了handler的 ...

  9. 64位FreeSWITCH编译安装(版本1.4.20)

    1.安装64位的CentOS6.5操作系统(勾选的服务器版本安装). 2.下载FreeSWITCH安装包以及关联的lib库.下载地址http://files.freeswitch.org/downlo ...

  10. shell将脚本输出结果记录到日志文件

    使用tee命令: sh portal/main.sh |tee log.txt 获取脚本父类路径cmddir="`dirname $0`"