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: ...
随机推荐
- 第二百三十九节,Bootstrap路径分页标签和徽章组件
Bootstrap路径分页标签和徽章组件 学习要点: 1.路径组件 2.分页组件 3.标签组件 4.徽章组件 本节课我们主要学习一下 Bootstrap 的四个组件功能:路径组件.分页组件.标签组件 ...
- <! - - ... - -> 注解
<A HREF TARGET> 指定超连结的分割视窗 <A HREF=#锚的名称> 指定锚名称的超连结 <A HREF> 指定超连结 <A NAME=锚的名称 ...
- 图像处理之优化---任意半径局部直方图类算法在PC中快速实现的框架
在图像处理中,局部算法一般来说,在很大程度上会获得比全局算法更为好的效果,因为他考虑到了图像领域像素的信息,而很多局部算法可以借助于直方图获得加速.同时,一些常规的算法,比如中值滤波.最大值滤波.最小 ...
- kafka2:常用topic命令
常用操作 创建一个topic bin/kafka-topics.sh --create --zookeeper 192.168.3.230:2181 --replication-factor 3 -- ...
- mysql分表技术
一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法. 目前我所知道的方法都是MYISAM的,INNODB如何做分表并且保留事务和外键,我还不 ...
- 【转】Windows Dump文件获取
dump文件是进程的内存镜像.可以把程序的执行状态,即当时程序内存空间数据通过调试器保存到dump文件中. 1.利用WinDbg里的adplus来获取dump文件 Adplus.vbs 是一个Visu ...
- 【转】防止CListCtrl闪烁的几种方法
转载出处:http://blog.sina.com.cn/s/blog_5ee42ba30100g50j.html 1.使用SetRedraw禁止窗口重绘,操作完成后,再恢复窗口重绘 m_ctlLis ...
- spotlight on windows 监控
1. spotlight on windows 安装 下载 https://pan.baidu.com/s/1qYi3lec Spotlight大家可以从其官方网站(http://www.quest. ...
- java人民币转大写中文
代码如下: import java.math.BigDecimal; /** * @author andy * @create 2016-08-12 18:51 */ public class Pri ...
- 160727、自定义hibernate主键生成策略生成字符串+数字自增长
需求:需要自增长注解如MyId0001.MyId0002.MyId0003 实现:实现这个接口org.hibernate.id.IdentifierGenerator 一.MyIdGenerator. ...