【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters
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.
双指针begin,end.扩展end,收缩begin.
[begin,end]之间为无重复字母的子串。
可与Longest Substring with At Most Two Distinct Characters对照看。
解法一:
使用unordered_map记录每个字母最近出现下标。
[begin, end]表示无重复字符的窗口。
在end指向一个窗口中的字符时,需要将begin收缩到该字符之后,以免与end重复。
时间复杂度:O(n)
空间复杂度:O(n)
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s == "")
return ;
unordered_map<char, int> m; //char-index map
int ret = ;
int begin = ;
m[s[begin]] = ;
int end = ;
while(end < s.size())
{
//extend
if(m.find(s[end]) == m.end() || m[s[end]] < begin)
{
;
}
//shrink
else
{
begin = m[s[end]]+;
}
ret = max(end-begin+, ret);
m[s[end]] = end;
end ++;
}
return ret;
}
};

解法二:
使用record[128]记录A~Z,a~z在窗口中的出现次数。
由于无重复的要求,因此在end指向一个已经出现过的字母(即record[s[end]]不为零)
需要收缩begin直到begin遇到end或者record[s[end]]为零。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int longest = ;
int begin = ;
int end = ;
int record[] = {}; //initial to all 0
while(end < s.size())
{
while(record[s[end]]> && begin<end)
{
record[s[begin]] --;
begin ++;
}
//record[s[end]]==0 || begin==end
record[s[end]] ++;
longest = max(longest, end-begin+);
end ++;
}
return longest;
}
};

【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)的更多相关文章
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- leetcode-【中等题】3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- JAVA动态编译(JavaCompiler)
一.简介 在java中javax报下提供了JavaCompiler类,此类可以允许开发人员编译java文件为class文件. 下面示例中是利用JavaCompiler编译文件,并利用URLClassL ...
- Linux动态链接库的使用
1.前言 在实际开发过程中,各个模块之间会涉及到一些通用的功能,比如读写文件,查找.排序.为了减少代码的冗余,提高代码的质量,可以将这些通用的部分提取出来,做出公共的模块库.通过动态链接库可以实现多个 ...
- 什么是BFC(Block Formatting Context)
原文:https://segmentfault.com/a/1190000012221820 https://www.w3.org/TR/CSS2/visuren.html#block-formatt ...
- express统一输出404页面
不玩不知道,一玩吓一跳,还真是,nodejs全局404怎么搞? 直接,res.render("404.html")有可能会报错:Node.js : Cannot find modu ...
- BitNami
BitNami 提供wordpress.joomla.drupal.bbpress等开源程序的傻瓜式安装包下载,所有的安装包内置了服务器环境,就是说,不需要在本地 电脑上另外搭建服务器,就可以一次性傻 ...
- Javascript中变量函数申明优先级
先理解这句话: “函数会首先被提升,然后才是变量”,代码并不是你写的顺序那样执行的. F12把下面的代码粘贴到控制台执行一下: var getName = function () { console. ...
- SuperMap打包部署要点
折腾了一段时间,终于要发布一个版本了,但SuperMap程序怎么发布呢,需要些什么必要条件呢?本来想问问超图的技术人员的,但都没人理我,估计都去开大会去了. 下面是自己测试出来的结果,主要是根据Sup ...
- jquery如何获取元素的滚动高度
获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 : $(window).width(); 获取页面的文档高度 $(documen ...
- 在linux下makefile的使用
在linux下makefile的使用
- linux测试工程介绍(Linux Test Project)
http://ltp.sourceforge.net/ Linux Test Project, 后台很硬,由SGI™ 发起, IBM维护,所以质量有保障. 里面介绍了很多工具,对于一般的基准测试应该是 ...