我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/

003. Longest Substring Without Repeating Characters[M]

题目

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

思路

题目意思是在一个字符串中找一个最长的子串(没有重复的字母)
简单的思路:
从左往右扩展子串,维持2个变量i和j来维持一个新的子串,j不断移动,每加入一个新的字符,判断是否有重复的,如果有重复的,移动i,生成新子串……
len = max(len,j-i+1);





这里有个问题就是,如何判断子串中是否有重复的字符,传统思路就是循环,这样每次查找重复的时间复杂度为O(n),导致整体时间复杂度为O(N^2),其实我们可以使用hashmap来存,这样可以保证每次查找的效率为O(1)。

代码

class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int> mymap;
unordered_map<char,int>::iterator it;
int len = 0,i = -1;
for(int j=0;j < s.length();j++)
{
/***是否有重复******/
it = mymap.find(s.at(j));
if(it != mymap.end())
/*****有重复的时候,移动i*****/
i = std::max(it->second,i);
/****把新的字符加入*******/
mymap[s.at(j)] = j;
len = std::max(len,(j-i));
}
return len;
}
};

但是实际上,对于这个题目,不需要用hashmap,因为所有的字符ASCII码加起来也就最多255个,可以直接用数组来代替hashmap,效率更高。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> mymap(255,-1);
int len = 0,i = -1,tmp;
for(int j=0;j < s.length();j++)
{
tmp = mymap[s.at(j)];
i = std::max(tmp,i);
mymap[s.at(j)] = j;
len = std::max(len,(j-i));
}
return len;
}
};

上面用了一个trick就是每个数组的初始化为-1表示没有出现重复,它不可能比i的初始值大,如果有重复的,直接覆盖,这样可以不用额外的语句判断是否出现重复。

《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复的更多相关文章

  1. leetcode题解 3. Longest Substring Without Repeating Characters

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

  2. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

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

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  4. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  5. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  6. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  7. 【LeetCode】003. Longest Substring Without Repeating Characters

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

  8. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

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

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

随机推荐

  1. Max Sum -- hdu -- 1003

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  2. ZOJ 3702 Gibonacci number 2017-04-06 23:28 28人阅读 评论(0) 收藏

    Gibonacci number Time Limit: 2 Seconds      Memory Limit: 65536 KB In mathematical terms, the normal ...

  3. (轉)CSS 单行溢出文本显示省略号...的方法(兼容IE FF)

    轉自:http://www.cnblogs.com/hlz789456123/archive/2009/02/18/1392972.html html代码:<div><p>&l ...

  4. mysql数据表简单拷贝及重命名

    CREATE TABLE to LIKE from;//拷贝结构 RENAME TABLE from TO to;//重命名

  5. CodeIgniter使用中写的一些文章

    CI的captcha替代类库:  http://www.ifixedbug.com/posts/codeigniter-captcha-library 原生的captcha不是太好用,自己组装一个吧. ...

  6. [idea]对于前端里面一些事情的看法

    一些是自己的想法,另一些是看博客或者书籍里面得出的,随手记在这里. 基于页面的开发 最初的前端资源模式是基于页面的,像最开始接触web.开发web时候,一般是新建页面-引入jQuery-新建index ...

  7. R语言中Fisher判别的使用方法

    最近编写了Fisher判别的相关代码时,需要与已有软件比照结果以确定自己代码的正确性,于是找到了安装方便且免费的R.这里把R中进行Fisher判别的方法记录下来. 1. 判别分析与Fisher判别 不 ...

  8. ubuntu 16.04.1 nginx彻底删除与重新安装

    1.删除nginx,-purge包括配置文件 sudo apt-get --purge remove nginx 2.移除全部不使用的软件包 sudo apt-get autoremove 3.罗列出 ...

  9. Windows10 下 github ssh 访问出现 Permission denied(publickey)错误的解决方法

    Windows10 下 github ssh 访问出现 Permission denied(publickey)错误的解决方法. 错误信息: git clone git@github.com:ediw ...

  10. ANE-如何加入ane,调试时又不报错

    有时候我们加入ane,即使没有调用ane的功能,debug的时候也会报错无法调试,这是为什么呢?因为我们的ane没有把default包含进去. 首先我们的extension.xml要把default节 ...