3. Longest Substring Without Repeating Characters(最长子串,双指针+hash)
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
暴力:时间0(n2)
空间o(n)
class Solution {
public:
int lengthOfLongestSubstring(string s) {
const int n = s.size();
std::unordered_set<char> s_set;
int res = ;
for (int i = ; i < n; i++) {
int j = i;
s_set.clear();
for (; j < n ;++j) {
if (s_set.find(s[j])==s_set.end()) {
s_set.emplace(s[j]);
} else {
break;
}
}
res = std::max(res,j-i);
}
return res;
}
};
class Solution {
public:
int lengthOfLongestSubstring(string s) {
const int n = s.size();
std::unordered_set<char> s_set;
int res = ;
int j = ;
for (int i = ; i < n;++i) {
while(s_set.find(s[i])!=s_set.end()) {
s_set.erase(s[j]);
j++;
}
res = std::max(res,i-j+);
s_set.emplace(s[i]);
}
return res;
}
};
这个题的关键在于重复的字符!
一个子串不可能出现重复的字符,那么我们可以用两个指针,一个指针用来遍历字符串,两个指针之间保持无重复字符,那么两个指针之间的长度即最大子串的长度。当发现有重复的字符时,另一个指针指向这个字符上一次出现的位置的下一个字符,继续遍历,直到找到最长子串。


class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
j = 0
maxlen = 0
used ={}
for i ,c in enumerate(s):
if c not in used or used[c] < j:
maxlen = max(maxlen,i-j + 1)
else:
j = used[c] + 1
used[c] = i
return maxlen
代码中:
used[c] < start 是为了处理"tmmzuxt" 这种连续出现的2个字母。
3. Longest Substring Without Repeating Characters(最长子串,双指针+hash)的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
随机推荐
- Centos7安装Apache Http服务器无法访问如何解决
1. 安装Apache组件 [root@mycentos shell]# yum install httpd 2. 安装成功后,检测有无httpd进程 [root@mycentos shell]# p ...
- find命令结合cp bash mv 命令使用的4种方式
工作经常需要用find结合其它命令一起使用,下面介绍4种结合方式. 例: 用find查找/data目录下,以.txt文件结尾的文件并复制到/tmp下 方法一 find与|xargs是黄金搭档,-t 参 ...
- 消耗资源的SQL的定位方法;
解答:select sql_text from v$sql where disk_reads > 1000 or (executions > 0 and buffer_gets/execu ...
- express搭建权限管理系统
express搭建权限管理系统 权限管理,是管理系统中的常见组件.通常需要定义资源,把资源调配给用户,通过判断用户是否有权限增删改查来实现. 初衷: 使用express开发过的项目大大小小加在一起也有 ...
- 应用程序无法正常启动 0x0000005
FeiQ应用程序无法正常启动了,错误代码0x0000005 右键FeiQ.exe,[属性],以Windows7兼容模式运行~
- hdu 4067(最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4067 思路:很神奇的建图,参考大牛的: 如果人为添加t->s的边,那么图中所有顶点要满足的条件都 ...
- homebrew常用指令
其它Homebrew指令: brew list —列出已安装的软件 brew update —更新Homebrew brew home *—用浏览器打开 brew info *—显示软件 ...
- mac下面安装多个JDK
JDK8 GA之后,小伙伴们喜大普奔,纷纷跃跃欲试,想体验一下Java8的Lambda等新特性,可是目前Java企业级应用的主打版本还是JDK6, JDK7.因此,我需要在我的电脑上同时有JDK8,J ...
- CodeForces 157A Game Outcome
A. Game Outcome time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- ssh访问跳过RSA key"yes/no"验证
通常我们再批量配置多台机器的时候经常出现通过ssh批量登录机器提示 RSA key fingerprint is ::a6:b1:c9:d7:b8::c1:::8e:f5::2b:8b. Are yo ...