Longest Substring Without Repeating Characters - 哈希与双指针
题意很简单,就是寻找一个字符串中连续的最长包含不同字母的子串。
其实用最朴素的方法,从当前字符开始寻找,找到以当前字符开头的最长子串。这个方法猛一看是个n方的算法,但是要注意到由于字符数目的限制,其实这是个O(Cn)的算法,最长也不过是C长度。所以我觉得普通方法应该是能过的。
于是写了一个,字符数目最大也不超过256所以代码如下:
class Solution {
public:
int lengthOfLongestSubstring(string s) { int res=;
for(int i=;i<s.length();i++)
{
int flag[];
memset(flag,,sizeof(flag));
int count = ;
flag[s[i]-' '] = ;
int tempres = ;
for (int j = i + ; j < s.length(); j++) {
if (flag[s[j]-' ']==) {
flag[s[j]-' ']= ;
tempres++;
} else
break;
}
if (tempres > res)
res=tempres;
}
return res;
}
};
其中空格是ASCII码第一个实际意义字符,所以减去‘ ’
正常的O(n)方法是使用哈希表来存已经出现的字符,使用一个指针依次检索,如果碰到已经存在的字符,则去使用第二个指针更新这个哈希表。
从原理上来具体讨论双指针这个算法,对于第一个指针指到的字母有两种情况:
1.从来没有使用过
当前长度加一,将这个位置和字母加入哈希表,第二个指针不动
2.已经使用过,并且有一个哈希表存这个字母的上一个位置
获取上一个位置右侧位置(+1操作),将这个位置和第二个指针比较,
(1)如果小于第二个指针,说明以当前结尾的字符串在第j个指针的地方有字母重复,忽略这个位置,继续以j为准
(2)如果大于第二个指针,说明以当前结尾的字符串在这个最新的位置有重复,将指针移到这个位置,计算这个长度。
这三种情况涵盖了所有可能,并在下面的例子中有相应出现。
代码:
此代码来自最多discuss区最多vote的答案:https://leetcode.com/discuss/23883/11-line-simple-java-solution-o-n-with-explanation
public int lengthOfLongestSubstring(String s) {
if (s.length()==0) return 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int max=0;
for (int i=0, j=0; i<s.length(); ++i){
if (map.containsKey(s.charAt(i))){
j = Math.max(j,map.get(s.charAt(i))+1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i-j+1);
}
return max;
}
例子:
对于这个字符串:abcbcda
经过初始化循环执行过程如下:
i=0; j=0; (a,0) max(0,1)=1 第一种情况
i=1; j=0; (b,1) max(1,2)=2 同上
i=2; j=0; (c,2) max(2,3)=3 同上
i=3; j=(0,1+1)=2; (b,3) max(3,2)=3 第二种第二个情况
i=4; j=(2,2+1)=3; (c,4) max(3,2)=3 同上
i=5; j=3; (d,5) max(3,3)=3 第一种情况
i=6; j=(3,1)=3; (a,6) max(3,4)=4 第二种第一个情况
Longest Substring Without Repeating Characters - 哈希与双指针的更多相关文章
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
- Longest Substring Without Repeating Characters (c#)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode3:Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- 3.Longest Substring Without Repeating Characters(string; HashTable)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]
题目 Given a string, find the length of the longest substring without repeating characters. Examples: ...
- LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...
随机推荐
- Http record java
http://httpunit.sourceforge.net/doc/servletunit-intro.html https://code.google.com/p/http-impersonat ...
- Javascript 风格向导
序 大部分针对Javascript最合理的方法归纳. 类型 • 原始类型:我们可以直接使用值. ο string ο number ο boolean ο null ο undefined ...
- 成都Uber优步司机快速注册攻略(外地车牌也可加入,不用现场培训)
我加入Uber司机有一段时间了,有一些经验和感想分享给大家,让大家少走些弯路.目前加入优步不收取任何费用,不需要抢单,时间安排自由灵活,使用便捷,深受大众喜爱. 加入人民优步拼车条件:购买运行5年之内 ...
- stringstream字符串流
例题详解 題目:输入的第一行有一个数字 N 代表接下來有 N 行資料,每一行資料里有不固定個數的整數(最多 20 個,每行最大 200 個字元),請你寫一個程式將每行的总和印出來. 輸入: 3 1 2 ...
- android项目 之 记事本(12) ----- 图片的等比例缩放及给图片加入边框
本文是自己学习所做笔记.欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在Android的UI开发中常常会遇到图片的缩放,就比方记事本,如今的图片都比較 ...
- Cocos2d-X 动作展示《一》
因为Cocos2d-X中的动作较多,我将全部的动作制作成了一个滚动视图.每一个滚动视图上都有动作名,单击滚动视图就能够展示对应的动作 程序效果图: 使用滚动视图实现动作切换 动作展示 程序代码: 首先 ...
- css3的一些属性
以前还没有注意过css的一些属性,近期发现有一些样式很好用,现在整理一遍. CSS3 动画属性 @keyframes : 规定动画 可以通过keyframes 改变一个块的样式当然这是要配合anima ...
- objective-C学习笔记(七) 字符串处理
字符串NSString NSString 是一个Unicode编码,16位字符的字符序列. NSString 是一个类,拷贝时需要注意. 初始化方法:字面量初始化.初始化器.工厂方法. NSStrin ...
- CodeForces 577A Multiplication Table 质因子数
题目:click here 题意:看hint就懂了 分析:数论小题,在n0.5时间里求n的质因子数 #include <bits/stdc++.h> using namespace std ...
- 一个简单顺序表的C++实现
/* SList.cpp Author: Qiang Xiao Time: 2015-07-11 */ #include<iostream> using namespace std; ; ...