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

思路:使用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. HDU 1151 Air Raid(最小路径覆盖)

    题目大意: 有n个城市,m条道路,城市的道路是单向.  现在我们的伞兵要降落在城市里,然后我门的伞兵要搜索所有道路.问我们最少占领多少个城市就可以搜索所有的道路了. 我们可以沿着道路向前走到达另一个城 ...

  2. 图论(Tarjan缩点):BZOJ 1194: [HNOI2006]潘多拉的盒子

    1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 344  Solved: 181[Submit][Stat ...

  3. IIS6.0服务器搭建网站无法访问解决方法

    IIS6.0服务器搭建网站无法访问解决方法     IIS6.0服务器搭建网站无法访问解决方法很多朋友在用IIS6架网站的时候遇到不少问题,而这些问题有些在过去的IIS5里面就遇到过,有些是新出来的, ...

  4. bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 484  Solved: 296[Submit][Status ...

  5. Krypton Factor 困难的串-Uva 129(回溯)

    原题:https://uva.onlinejudge.org/external/1/129.pdf 按照字典顺序生成第n个“困难的串” “困难的串”指的是形如ABAB, ABCABC, CDFGZEF ...

  6. Marzoni(玛佐尼)意大利顶级西服面料之一_HollandandSherry_新浪博客

    Marzoni(玛佐尼)意大利顶级西服面料之一_HollandandSherry_新浪博客 Marzoni(玛佐尼)意大利顶级西服面料之一 (2013-01-08 17:30:04) 转载▼

  7. GitLib

    http://www.360doc.com/content/15/0603/14/21631240_475362133.shtml 原文 http://blog.csdn.net/williamwan ...

  8. 【微信公众号】将微信公众号消息里的FromUserName即OpenID转成UnionID

    最近在调试微信公众号开发者模式,处理公众号消息,收到如下回调消息内容 <xml><ToUserName><![CDATA[gh_29********21]]>< ...

  9. Linq to sql并发与事务

    本文转载:http://www.cnblogs.com/lovecherry/archive/2007/08/20/862365.html 检测并发 首先使用下面的SQL语句查询数据库的产品表: se ...

  10. Team Foundation Server 2013 with Update 3 Install LOG

    [Info   @10:14:58.155] ====================================================================[Info   @ ...