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: ...
随机推荐
- 数据结构(块状链表):COGS 1689. [HNOI2010]Bounce 弹飞绵羊
时间限制:1 s 内存限制:259 MB [题目描述] 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地 ...
- 动态规划(斜率优化):SPOJ Commando
Commando You are the commander of a troop of n soldiers, numbered from 1 to n. For the battle ahead, ...
- CodeForces 689A -Mike and Cellphone
题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=412142 题目大意: 给定一个0-9数字键盘,随后输入一个操 ...
- WCF分布式事务
原文地址:http://developer.51cto.com/art/201002/185426.htm 我们作为一个开发人员,应该能够顺应技术的不断发展,不断的去掌握新技术.那么,对于WCF的掌握 ...
- Appium移动自动化测试(三)--安装Android模拟器(转)
Appium移动自动化测试(三)--安装Android模拟器 2015-06-08 10:33 by 虫师, 30828 阅读, 9 评论, 收藏, 编辑 当Android SDK安装完成之后,并不意 ...
- logback 配置详解(一)(转)
转自:http://blog.csdn.net/haidage/article/details/6794509/ 一:根节点<configuration>包含的属性: scan: 当此属性 ...
- SimpleDateFormat线程不安全问题处理
在工作中,通过SimpleDateFormat将字符串类型转为日期类型时,发现有时返回的日期类型出错,调用方法如下: public final class DateUtil { static fina ...
- iOS中ASI和AFN的区别
一.底层实现 1> AFN的底层基于OC的NSURLConnection和NSURLSession 2> ASI的底层基于纯C语言的CFNetwork框架 3> ASI的运行性能 高 ...
- LinearLayout增加divider分割线
在android3.0及后面的版本在LinearLayout里增加了个分割线 1 2 android:divider="@drawable/shape"<!--分割线图片-- ...
- CentOS 安装 Tomcat
1.Tomcat官网获(http://tomcat.apache.org/)取tar.gz文件的下载地址 2.下载: # wget http://apache.fayea.com/tomcat/tom ...