【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, ...
随机推荐
- laravel5.5 dingo/api+jwt-auth
因为laravel5.5 具有发现包功能,只要包做了兼容laravel5.5就可以不用在config/app.php添加额外代码了. 集成dingo/api github:https://github ...
- awk的基本使用方法
awk是处理文本文件的一个应用程序,几乎所有 Linux 系统都自带这个程序. 它依次处理文件的每一行,并读取里面的每一个字段.对于日志.CSV 那样的每行格式相同的文本文件,awk可能是最方便的工具 ...
- Java归去来第1集:手动给Eclipse配置Maven环境
一.Eclipse配置Maven 1.1.下载Maven http://maven.apache.org/download.cgi,选择对应的版本,window下载apache-maven-3.5.3 ...
- (转)【风宇冲】Unity3D教程宝典之AssetBundles:第一讲
自:http://blog.sina.com.cn/s/blog_471132920101gz8z.html 原创文章如需转载请注明:转载自风宇冲Unity3D教程学院 ...
- URL重写2.1.mis
概观 IIS URL重写2.1使Web管理员能够创建强大的规则来实现更容易让用户记住的网址,并使搜索引擎更容易找到.通过使用规则模板,重写映射,.NET提供程序和集成到IIS管理器中的其他功能,Web ...
- HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)
传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 迁移Windows下的MySQL时字符乱码问题
我们常常会直接复制一份MySQL的Data文件夹到新的环境下,正常情况下重新启动MySQL就可以使用.但有时也会遇到些问题: 1.程序訪问时提示找不到表,实际表已经存在 这样的情况是因为数据库全部者可 ...
- HTTP协议综合
1.用浏览器模拟各种User Agent 测试页面的时候经常需要不同的User Agent,Firefox.Chrome浏览器就可以完美的模拟出各种User Agent.User Agent Swit ...
- Redis 操作数据
展现最新数据 Web应用常常要展现最新数据,就会根据时间对数据排序: SELECT * FROM foo WHERE ... ORDER BY time DESC LIMIT 10 随着数据的增加,问 ...
- VB.NET版+三层实现登陆
三层已经学了一些时间了,開始认为自己能够用C#敲代码了,就用C#写了一个实现登陆的,真正再用在机房中.还是认为非常吃力的,所以.决定用vb.net敲了.以下是我用vb.net实现的登陆.能够给大家做一 ...