3. Longest Substring Without Repeating Character[M] 最大不重复子串
题目
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input:"abcabcbb"
Output:3
Explanation:The answer is "abc",with the length of 3.
Example 2:
Input:"bbbb"
Output:1
Explanation:The answer is "b",with the length of 1.
Example 3:
Input:"pwwkew"
Output:3
Explanation: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.
思路
思路1:滑动窗口法
首先分析题意,这题是求一个最长不重复子串,需要注意两点:一是不重复;二是子串,不是子序列,这意味着字符必须连续。
如何想到滑动窗口的方法?首先考虑采取最暴力的方法找出字符串s="abcabcbb"的最大不重复字串,就是一个字符一个字符的遍历,"a","b","c"三个不重复,这时候最大不重复子串为"abc",当遍历到第四个字符a的时候,发现与前面字符串有重复。由于我们不知道后面是否还有不重复字符,为了保持字符的连续,此时的最大不重复子串应为"bca",同理"cab","abc"。这样就形成了一个滑动窗口。
现在,我们有了目标,寻找一个窗口,这个窗口内包含都是不重复的字符,现在的目标是使得该窗口最大。
为了计算窗口的长度,我们需要一个left值来表示窗口的左边界。为了验证验证窗口内的字符与当前正在遍历的字符是否一致,需要记录出现过的字符;为了形成窗口以及移动窗口,需要记录字符的下标,因此我们需要建立一个字符与其出现位置之间的映射,C++中的哈希表,python中的字典可以实现这一点。
接下来,我们需要维护窗口。由于窗口是向右移动,所以我们需要记录的是字符最后出现的下标。如果当前遍历的字符没有出现过,扩大右边边界,将当前字符加入窗口。如果当前当前字符出现过,分为两种情况
- 在窗口内:去掉重复的字符。具体由将左边界(left)移动到重复字符的位置;
- 不在窗口内:窗口内加入该字符即可。
思路2:滑动窗口法优化
用ASCII表中字符与十进制整数之间的映射,来代替思路一通过哈希表建立的字符与该字符在字符串中最后出现的位置之间的映射。由于ACSII表共能表示256个字符,因此我们可以建立一个256维的数组来代替哈希表,并将数组的每个元素初始化为-1。这样就不用查找字符的映射,对于遍历到的每个字符,更新他在数组对应位置(根据ASCII表将字符转化为对应的int),再用数组的元素来更新窗口的左边界。这样,不用专门建立映射,而是根据ASCII表字符与整数的映射,加快速度。
C++
- 思路1
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int> map; //建立字符与该字符在s中最后出现的位置之间的映射
int left = -1; // 左边界,指向窗口的前一个位置
int result = 0; //长度
for(int i = 0;i < s.size(); i++){
//count:判断当前字符s[i]是否出现过
//map[s[i]]>left:如果当前遍历的字符出现过,并且在窗口内。
//map并不是窗口,而是记录字符字符与最后出现位置的下标之间的映射,left用来维护窗口
if(map.count(s[i]) && map[s[i]] > left)
left = map[s[i]];
map[s[i]]=i;
result = max(result, i-left);
}
return result;
}
};
- 思路2
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> map(256,-1);
int left = -1;
int result = 0;
for(int i = 0;i < s.size(); i++){
if(map[s[i]] > left)
left = map[s[i]];
map[s[i]]=i;
result = max(result, i-left);
}
return result;
}
};
Python
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
table = { }
left = -1
result = 0
for i in range(len(s)):
if (s[i] in table) and table[s[i]] > left:
left = table[s[i]]
table[s[i]] = i
result = max(result, i - left)
return result
3. Longest Substring Without Repeating Character[M] 最大不重复子串的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [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: ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
- 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, ...
- Longest Substring Without Repeating Characters 最长不重复子串
只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...
- length of the longest substring without repeating character
Given a string, find the length of the longest substring without repeating characters. 来源:力扣(LeetCod ...
随机推荐
- java8 Stream 笔记
stream的定义:对一个源中的一系列元素进行聚合操作. 一系列元素:stream对一组有特定类型的元素提供了一个接口.但是stream并不真正存储元素,元素根据需求被计算出来. 源:stream可以 ...
- 【Oracle】创建概要文件
任务1:创建profile 创建概要文件my_profile 1)密码复杂性要求:启用: 2)密码长度最小值:8位: 3)密码错误输入三次,锁定账户,2分钟后自动解锁 --创建密码复杂度校验函数 @? ...
- Redis入门笔记-redis内部数据结构(01)
redis是一个轻量级的Nodsql数据库,使用kev-value的形式存储数据,在redis的世界里,没有整数.浮点数等概念,大多数情况下数据以字符串形式展现,偶尔会出现Long类型数据的场景. 一 ...
- 可以忽略的:BASH:/:这是一个目录
linux Ubuntu 14.04 在使用VIM编辑 /etc/profile 保存之后,出现了这个问题 其实,这个是可以忽略不计的问题,字符编码问题
- java学习笔记1——继承
通过在类的声明中加入extends子句创建一个子类并继承父类的成员变量和方法.如: class SubClass extends SuperClass{ ... } 若SuperClass是另一个类的 ...
- php常用方法一
1.用户名用***替换 /** * 用户名中间用***替换 * @param string $str 需要替换的字符串 * @param int $len 需要替换的位数 * @param strin ...
- 接口测试及Postman工具
首先,什么是接口呢? 接口一般来说有两种,一种是程序内部的接口,一种是系统对外的接口.系统对外的接口:比如你要从别的网站或服务器上获取资源或信息,别人肯定不会把数据库共享给你,他只能给你提供一个他们写 ...
- IIS访问php页面问题,报告404错误
IIS访问php页面出现问题,所有php页面找不到,显示404页面,html页面可以正常访问. 搜索结果: 方案1: 打开WEB服务扩展,把“所有未知ISAPI扩展”设为允许! 方案2: PHP没有完 ...
- Project Euler 43 Sub-string divisibility
题意: 1406357289是一个0至9全数字数,因为它由0到9这十个数字排列而成:但除此之外,它还有一个有趣的性质:子串的可整除性.记d1是它的第一个数字,d2是第二个数字,依此类推,我们注意到: ...
- N3-1 - 数组 - convert-sorted-array-to-binary-search-tree
题目描述: Given an array where elements are sorted in ascending order, convert it to a height balanced B ...