3 Longest Substring Without Repeating Characters(最长不重复连续子串Medium)
题目意思:求字符串中,最长不重复连续子串
思路:使用hashmap,发现unordered_map会比map快,设置一个起始位置,计算长度时,去减起始位置的值
eg:a,b,c,d,e,c,b,a,e
0 1 2 3 4
0 1 5 3 4
0 6 5 3 4
7 6 5 3 4
7 6 5 3 8
再次出现c时,将start值置为map[c]+1=3,对于下一个b,因为map[b]<start,则直接map[b]=i
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int> mymap;
int flag=;
int start=,i=;
unordered_map<char,int>::iterator ite;
while(i<s.size()){
ite=mymap.find(s[i]);
if(ite==mymap.end()){
mymap[s[i]]=i;
flag=max(flag,i-start+);
}
else{
if(mymap[s[i]]>=start)
start=mymap[s[i]]+;
mymap[s[i]]=i;
flag=max(flag,i-start+);
}
++i;
}
return flag;
}
};
时间复杂度:O(n)
超时解法:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char,int> mymap;
int i=,max=,flag;
map<char,int>::iterator ite;
while(i<s.length()){
ite=mymap.find(s[i]);
if(ite==mymap.end()){
mymap[s[i]]=i;
if(mymap.size()>max)
max=mymap.size();
}
else{
flag=ite->second;
mymap.clear(); //每次清空,是及其失败的做法
for(int j=flag+;j<=i;++j){
mymap[s[j]]=j;
}
}
++i;
}
return max;
}
};
3 Longest Substring Without Repeating Characters(最长不重复连续子串Medium)的更多相关文章
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- [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 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- [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 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
随机推荐
- -_-#【JS】isFinite
/** * isFinite(number) * 检查是否是无穷大 * 如果 number 是有限数字(或可转换为有限数字),那么返回 true * 如果 number 是 NaN(非数字),或者是正 ...
- Qt入门(19)——自定义窗口部件
我们介绍可以画自己的第一个自定义窗口部件.我们也加入了一个有用的键盘接口.我们添加了一个槽:setRange(). void setRange( int minVal, int maxV ...
- Ubuntu 14.04 dnw配置
之前写的Ubuntu嵌入式环境搭建没有讲怎么配置dnw下载工具,使用dnw还得用红帽,今天配置好了ubuntu下的dnw,记录一下 首先先下载dnw的源码,这是我上传的提供给大家下载:http://p ...
- 从SG函数浅谈解决博弈问题的通法
基于笔者之前对于几种二元零和博弈游戏的介绍,这里将其思想进行简单的提炼,并引出解决这类二元零和博弈游戏的强大工具——SG函数. 其实对于博弈游戏如Bash.Nim等基本类型,异或一些比较高级的棋类游戏 ...
- poj1026
题目大意:暗号 Bod 和 Alice 计划使用一种全新的编码方案,令人惊讶的是这不是一个公开的公匙密码,但是他们的编码基于密匙,在Philadelphia on February 16th他们的会议 ...
- 初次了解CSS3
什么是CSS,什么是CSS3? 非常简单,CSS代表"Casading Style Sheets",就是样式表,是一种替代并为网站添加样式的标记性语言.现在所使用的CSS基本是在1 ...
- mybatis 关联对象mapper.xml的写法
https://github.com/zfrHJ/mybaties/blob/master/mybaties/src/com/itheima/mybatits/mapper/OrdersMapperC ...
- ZOJ 1301 The New Villa (BFS + 状态压缩)
题意:黑先生新买了一栋别墅,可是里面的电灯线路的连接是很混乱的(每个房间的开关可能控制其他房间,房间数<=10),有一天晚上他回家时发现所有的灯(除了他出发的房间)都是关闭的,而他想回卧室去休息 ...
- Android源代码分析之Framework的MediaPlayer
在Android中MediaPlayer用来播放音频和视频文件,在这里分析下在Framework层中MediaPlayer是怎样调用的.MediaPlayer的代码位于:./frameworks/ba ...
- MYSQL 体系结构图-space结构图