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: ...
随机推荐
- 编写可维护的JavaScript----笔记(一)
1.缩进层级 建议使用4个空格为一个缩进层级,避免使用制表符进行缩进,可以通过配置文本编辑器来改变 缩进层级表示的内容. 2.语句末尾 有赖于分析器的自动分号插入机制(ASI),JavaScript可 ...
- java项目的部署
1.将tomocat解压到服务器上 2.放项目war包. 3.war包解压. 4.修改端口配置. 1.<Server port="8024" shutdown="S ...
- pycharm重置设置,恢复默认设置
备忘,备忘,备忘 window 系统 找到下方目录-->删除. 再重新打开pycharm # Windows Vista, 7, 8, 10: <SYSTEM DRIVE>\User ...
- js 滚动页面
$(‘html, body’).animate({ scrollTop: 0}, ‘slow’);
- CentOS安装Oracle官方JRE
CentOS自带的JRE是OpenJDK,因为一些原因,需要换用Oracle官方出品的JRE. Oracle JRE下载地址http://java.com/zh_CN/(下载时注意选择相应版本) 可以 ...
- MySQL服务停止,无法启动了~
怎么解决mysql服务无法启动的问题
- [LintCode] 全排列
递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of permutati ...
- Oracle实例的恢复、介质恢复( crash recovery)( Media recovery)
实例的恢复( crash recovery) 什么时候发生Oracle实例恢复? shutdown abort; 数据库异常down掉(机器死机,掉电...) 实例恢复的原因是数据有丢掉,使用redo ...
- 系统根据用户cookies,为用户打上各种标签
DSP营销学院_品友学院 | 品友推广官网 http://e.ipinyou.com/school_article40.html 智能算法+动态出价=最大发挥推广费用的价值 针对每一个曝光进行甄别和竞 ...
- 微信支付 超时 mysql.event
$wtime 使用具体timestamp //rand 防推测 $wev = 'ev_gbuy_create_' . trim($winsert_id) . rand(100, 999); $sql ...