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 ...
随机推荐
- Yslow---一款很实用的web性能测试插件
YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. YSlow可以分析任何网站,并为每一个规则产生一个整体报告,如果页面可以进行优化,则YSlow会列出具体的修 ...
- struct timeval结构体 以及 gettimeofday()函数(转)
struct timeval结构体 转载地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html 该结构体是Linux系统中定义,struct ...
- Ubuntu菜鸟入门(十五)—— 安装aras2下载软件
一.安装arias2 sudo add-apt-repository ppa:t-tujikawa/ppa sudo apt-get update sudo apt-get install aria2 ...
- CentOS 开启防火墙 firewall ,mysql 远程访问
最近在阿里云服务器centos上安装了mysql数据库,默认是不开启远端访问功能,需要设置一下防火墙,在开放默认端口号 3306时提示FirewallD is not running,经过排查发现是防 ...
- 【Android】Android如何对APK反编译
本文笔者粗略的介绍如何利用一些工具,对Android进行反编译,从而得到源码,希望对你有所帮助,笔者的android环境为4.4.2. 1.准备资源. 在开始之前,需要准备三项工具:apktool ...
- 【Struts2】SSH如何返回JSON数据
在开发中我们经常遇到客户端和后台数据的交互,使用比较多的就是json格式了.在这里以简单的Demo总结两种ssh返回Json格式的数据 项目目录如下 主要是看 上图选择的部分 WebRoot里面就 ...
- SQL2000的系统表sysproperties在SQL2005中 无效的 问题
有两种解决办法 方法一.是我在网上找的:将原来的sysproperties改成sys.extended_properties并且对应关系如下 sys.extended_properties left ...
- AaronYang的留言板
^_^很开心能在这里遇到你,我是ay,英文名叫aaronyang,真名叫杨洋,安徽六安的,有老乡吗?这里的文章几乎都是我原创的,要不然就是收集别人的好的文章,自己再整理下与大家分享.绝对希望原创,本站 ...
- the most beautiful media player on the linux platform.
the most beautiful media player on the linux platform------> deepin media player http://wiki.linu ...
- Java Date and Calendar examples
Java Date and Calendar examples This tutorial shows you how to work with java.util.Date and java.uti ...