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: ...
随机推荐
- wpa_cli和wpa_supplicant使用,配置无线AP名和密码,静态ip地址
配置静态ip方法分享:通过串口命令行输入如下命令: 1. 添加无线网络接入点(AP) 及其 密码:# wpa_cli -p /data/misc/wpa_supplicantwpa_cli v0.5. ...
- (转载)PHP怎么获取MySQL执行sql语句的查询时间
(转载自CSDN) 方法一: //计时开始 runtime(); //执行查询 mysql_query($sql); //计时结束. echo runtime(1); //计时函数 function ...
- UVALive 5990 Array Diversit
题意:对于一个数列A,substring是一个连续子串,subsequence是其非连续子序列.对于一个数字序列,记它的diversity是它的最大元素减去最小元素的差.给出一个数字序列,求与它div ...
- python列表操作总结
list是python中非常重要的类型/数据结构,总结如下: 符号说明 l:列表 l2:新列表 e:元素 index:位置 方法: 原地修改: l.append(e) l.insert(index, ...
- C#快速剔除字符串中不合法的文件名或者文件路径字符
C#快速剔除字符串中不合法的文件名 string strFileName= "文件名称"; StringBuilder rBuilder = new StringBuilder( ...
- Android实现网络多线程断点续传下载(转)
本示例介绍在Android平台下通过HTTP协议实现断点续传下载. 我们编写的是Andorid的HTTP协议多线程断点下载应用程序.直接使用单线程下载HTTP文件对我们来说是一件非常简单的事.那么,多 ...
- Java基础知识强化之IO流笔记21:FileInputStream读取数据
1. 字节输入流的操作步骤: (1)创建字节输入流的对象 (2)调用read()方法读取数据,并把数据显示到控制台 (3)关闭字节输入流的对象资源 2. FileInputStream构造: File ...
- Java基础知识强化之IO流笔记07:自定义的异常概述和自定义异常实现
1. 开发的时候往往会出现很多问题(java内部系统框架中没有提供这些异常) 比如说:考试成绩必须在0~100之间. 很明显java没有对应的异常,需要我们自己来做一个异常. (1)继承自Except ...
- 剪切板 复制文本 ClipboardManager
代码 public class MainActivity extends ListActivity { private EditText tv_info; private Clipbo ...
- 9.5noip模拟试题
题目名称 正确答案 序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer.o ...