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 ...
随机推荐
- LayoutInflater源码解析
Android使用LayoutInflater来进行布局加载,通常获取方式有两种: 第一种: LayoutInflater layoutInflater = LayoutInflater.from(c ...
- OrmLite:no such table: tb_name
卸载原安装程序,从新安装(感觉是因为已经存在数据库,所以不执行创建.)
- element-ui Cascader 级联选择器示例
<html> <head>test</head> <style> @import url("http://unpkg.com/element- ...
- 01--Qt扫盲篇
Qt扫盲篇 1.What is Qt 一个跨平台应用程序和UI开发框架,主要偏向于UI框架方面,由诺基亚公司开发维护. 使用 Qt 只需一次性开发应用程序,无须重新编写源代码,便可跨不同桌面和嵌入式操 ...
- lambda表达式、匿名函数
lambda表达式是函数式编程中的匿名函数语法规范. In computer programming, an anonymous function (function literal, lambda ...
- Postfix Self Expression
Postfix Self Expression A postfix self expression consists of an expression or the name of a type, i ...
- 软件的描述x
1)文档描述: 2)模型描述: 3)架构描述: 4)代码描述:
- 前端手机验证码cookie存储
注册的时候经常会有手机验证码的输入这个环节,在第一次点击发送了验证码只后,比如倒计时只走了10秒钟,然后刷新的话,倒计时要还是存在的,这个时候就要有一个cookie的存在了. html的代码 < ...
- git基础讲解
idea :https://blog.csdn.net/autfish/article/details/52513465 eclipse:https://blog.csdn.net/yang57266 ...
- Pycharm 4.5.4 for ubuntu 16.04 下载与安装教程
首先,我们需要有一台已经安装好 ubuntu 16.04 的操作系统,并且配置好java环境: 方法1: 默认安装 apt-get install default-jdk -y # 安装官网最新的ja ...