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 ...
随机推荐
- [Offer收割]编程练习赛39
公平分队 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #includ ...
- PowerDesigner 逆向工程 Could not Initialize JavaVM!
原项目的大量的表,使用PowerDesigner 进行逆向工程.提示Could not Initialize JavaVM! 网上找到原因,PowerDesigner 不可以使用64位JDK环境! 有 ...
- spring中log4j的使用---转载
原文链接:http://www.codeceo.com/article/log4j-usage.html 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供 ...
- Kafka学习笔记(4)----Kafka的Leader Election
1. Zookeeper的基本操作 zookeeper中的节点可以持久化/有序的两个维度分为四种类型: PERSIST:持久化无序(保存在磁盘中) PERSIST_SEQUENTIAL:持久化有序递增 ...
- Java中数组遍历
就是将数组中的每个元素分别获取出来,就是遍历.遍历也是数组操作中的基石. 数组的索引是 0 到 lenght-1 ,可以作为循环的条件出现 public class ArrayDemo4 { publ ...
- JS棋盘
有一个棋盘,有64个方格,在第一个方格里面放1粒芝麻重量是0.00001kg, 第二个里面放2粒,第三个里面放4,棋盘上放的所有芝麻的重量 <!DOCTYPE html> <html ...
- android学习路线总结
感谢安辉作者,学习路线 https://www.cnblogs.com/yishaochu/p/5436094.html https://www.cnblogs.com/jycboy/p/60666 ...
- 探索JS引擎工作原理 (转)
这篇文章从相对底层的角度介绍了js引擎的工作 引入了 静态作用域 执行环境上下文(context) 等概念 , http://www.cnblogs.com/onepixel/p/5090799.ht ...
- UVA227 - Puzzle(紫书习题3.5)
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring&g ...
- [模板] zkw线段树
zkw线段树 code1简单版本 code2差分版本(暂无) code1:(有注释) //By Menteur_Hxy #include<cstdio> #include<iostr ...