【LeetCode】003. Longest Substring Without Repeating Characters
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.
题解:
设置left来标定现无重复字母的字符串的开始位置,i 表示遍历的位置,则无重复字母的最大子字符串的长度为 i - left + 1, 字典中存储的是字母与其对应的字符串的位置(以0为起始点)。变量res存储当前无重复字母的最大子字符串长度。需要注意的是这里不是先确定无重复字母的最大子字符串再求得其长度,而是确定每一个无重复字母的子字符串,求其长度,然后再与保存的最大长度作比较。相当于用一个数组不断的存储每一个无重复字母的字符串的长度,遍历完之后再从这些长度中取最大值作为结果,这里只不过是简化了操作,利用缓存记录上一无重复字母的子字符串的长度,然后不断与现无重复字母的子字符串的长度作比较,取最大值。所以,遇到区间类问题的最大最小值,可以先求得满足条件的所有情况,同时记录这些情况下的最大最小值结果,然后再对比这些结果取最大最小,或者用res = max(res, 现结果)来简化程序。
鉴于此题针对的字符串,可以建立一个数组来模拟字典,如果再遍历过程中map[s[i]]不等于-1, 说明此字母在 i 之前出现过:1. map[s[i]]<left, 说明之前的字母已经不在现非重复字符串中,所以仍然计算字符串长度。2. map[s[i]] >= left, 说明此字母在现字符串中已经出现过一次,则需要调整left的位置,具体调整为此字母上一次出现位置的下一位置,即left = map[s[i]] + 1;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> map(, -);
int res = , left = ;
for (int i = ; i < s.size(); ++i) {
if (map[s[i]] == - || map[s[i]] < left) {
res = max(res, i - left + );
} else {
left = map[s[i]] + ;
}
map[s[i]] = i;
}
return res;
}
};
利用set,把出现过的字符都放入set中,遇到set中没有的字符就加入set中并更新结果res,如果遇到重复的,则从左边开始删字符,直到删到重复的字符停止
class Solution {
public:
int lengthOfLongestSubstring(string str) {
set<char> s;
int res = , left = ;
for (int i = ; i < str.size(); ++i) {
if (s.find(str[i]) == s.end()) {
s.insert(str[i]);
res = max(res, i - left + ); // 也可以写作 res = max(res, (int)s.size()),此时的set中即是当前无重复字母的子字符串
} else {
s.erase(str[left++]);
--i;
}
}
return res;
}
};
【LeetCode】003. Longest Substring Without Repeating Characters的更多相关文章
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【JAVA、C++】LeetCode 003 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. Examples: ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
随机推荐
- four application:geocoder widget
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- [转]AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
AOP那些学术概念—通知.增强处理连接点(JoinPoint)切面(Aspect) 1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充 ...
- 虚构 css 父级选择器
能 CSS 解决的绝不用 JS,这句话又一次故作装逼地说出来还是挺爽的... 比如下拉列表,能用 CSS 的 :focus 就不用 JS 的 .on("focus blur") 能 ...
- 【转载】openwrt框架分析
文章出处:http://blog.csdn.net/kingvenll/article/details/27545221 这次讲讲openwrt的结构. 1. 代码上来看有几个重要目录package, ...
- Hearbeat + Nginx 安装配置
Hearbeat + Nginx 安装配置 实验环境 两台主机:Linux Centos 6.5 32位 主 服务端:Hearbeat + Nginx eth0:192.168.1.160(公网) e ...
- Kubernetes pod网络解析
在Kubernetes中,会为每一个pod分配一个IP地址,pod内的所有容器都共享这个pod的network namespace,彼此之间使用localhost通信. 那么pod内所有容器间的网络是 ...
- How does asp.net web api work?
https://hub.packtpub.com/working-aspnet-web-api/ https://docs.microsoft.com/en-us/aspnet/web-api/ove ...
- Linux下Python科学计算包numpy和SciPy的安装
系统环境: OS:RedHat5 Python版本:Python2.7.3 gcc版本:4.1.2 各个安装包版本: scipy-0.11.0 numpy-1.6.2 nose-1.2.1 lap ...
- window 下安装并启动zookeeper
1.下载zookeeper压缩包并解压大到磁盘中: 2.进入解压文件的: 3.进入conf,修改配置文件如下: 4.启动: 启动完成:
- linux基础(6)-shell编程
shell脚本 shell脚本程序:以文件形式存放批量的linux命令集合,该文件能够被shell释放执行.通常由一段linux命令.shell命令.控制语句以及注释语句构成. shell脚本特点: ...