题目意思:求字符串中,最长不重复连续子串

思路:使用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)的更多相关文章

  1. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

    Examples: Description: Given a string, find the length of the longest substring without repeating ch ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  5. LeetCode Longest Substring Without Repeating Characters 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

  6. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  8. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

随机推荐

  1. 转载:c++ sort用法

    sort函数使用模板: sort包含在头文件algorithm中 sort(start,end,排序方法) 1.在没有排序方法时是默认从小到大的排列,例 #include<iostream> ...

  2. json格式键盘编码对照表

    整理了一份JSON格式的键盘编码对照表.欢迎转载,但请注明出处,谢谢! { VK_BACK: 8, //退格键 VK_TAB: 9, //TAB键 VK_RETURN: 13, //回车键 VK_SH ...

  3. tcpCopy

    tcpcopy是一种应用请求复制(基于tcp的packets)工具,其应用领域较广,我们曾经应用于网易的广告投放系统,urs系统,nginx hmux协议开发等系统,避免了上线带来的很多问题. 总体说 ...

  4. pygame学习资料

    pygame下载地址: https://bitbucket.org/pygame/pygame/downloads 12岁的少年教你用Python做小游戏 Beginning Game Program ...

  5. MAVEN 工程打包resources目录外的更多资源文件

    首先,来看下MAVENx项目标准的目录结构: 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,ma ...

  6. C#与java中的集合区别

    集合一般的操作       插入: add       删除: remove       查找: contains,remove java中的集合 注意哪些是接口,哪些是实现类 使用集合的时候 1. ...

  7. android里Toast的用法

    在活动中,可以通过findViewById()方法获取到在布局文件中定义的元素,这里我们传入R.id.button_1,来得到按钮的实例,这个值是刚才在first_layout.xml中通过andro ...

  8. 火球-UML大战需求分析(体验版3.0.2).pdf

    火球-UML大战需求分析(体验版3.0.2).pdf http://files.cnblogs.com/files/happlyonline/%E7%81%AB%E7%90%83-UML%E5%A4% ...

  9. J2EE之WebLogic Server

    WebLogic是用于开发.集成.部署和管理大型分布式Web应用. 网络应用和数据库应 用的Java应用server. 将Java的动态功能和Java Enterprise标准的安全性引入大型网络应用 ...

  10. [PWA] 10. Trigger a version update

    When the refersh button is clicked, we need to tell the waiting service worker to replace the curren ...