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 ...
随机推荐
- javascript中构造函数的三种方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SurfaceView加载长图
1:SurfaceView加载长图,移到.可以充当背景 效果截图 2:View (淡入淡出动画没实现:记录下) package com.guoxw.surfaceviewimage; import a ...
- okhttp3 ExceptionInInitializerError 异常处理
okhttp3 在Android4.4上出现下面异常 java.lang.ExceptionInInitializerError at okhttp3.OkHttpClient.newSslSocke ...
- (转)微服务架构 互联网保险O2O平台微服务架构设计
http://www.cnblogs.com/Leo_wl/p/5049722.html 微服务架构 互联网保险O2O平台微服务架构设计 关于架构,笔者认为并不是越复杂越好,而是相反,简单就是硬道理也 ...
- oc懒加载 & swift lazy
oc的懒加载依赖于属性的双重属性的函数属性部分. 懒加载的本质是执行get函数. swift的lazy,理论上与此类似. 编译器优化时可能对初始化块进行了保存. 懒加载的本质是延迟执行. 只要是执行, ...
- MongoDB 学习笔记(一):安装及简单shell操作
一.说明 1.该系列MongoDB学习笔记的学习环境采用的MongoDB版本为mongodb-win32-i386-2.4.6,操作系统为win7. 二.安装 1.新建两个目录,分别是D:\Insta ...
- java 1.8 内存告警问题
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=512m; support was removed in 8.0 ...
- selenium工作原理
在我们new一个webdriver过程中 selenium会检测本地浏览器组件是否存在,版本是否匹配,接着会启动一套webservice ,这套webservice使用的selenium定义的webw ...
- webpack学习笔记(4)--webpack.config.js
devtool参数 这个参数控制是否生成,以及如何生成source map,已经在官网的doc说明总结了. 下表总结了各个参数和使用的情况 devtool 构建速度 重新构建速度 生产环境 品质(qu ...
- ELK介绍及搭建 Elasticsearch 分布式集群
上:https://blog.51cto.com/zero01/2079879 下:https://blog.51cto.com/zero01/2082794