【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 ...
随机推荐
- MSDN使用
比如我想查一下fopen这个函数怎么用,在索引里搜索一下fopen,很容易找到了. 但是如果我想横向扩展一下,查看一些与fopen相关的函数,应该怎么找呢? 很简单,点击定位: 你就能把fopen定位 ...
- css小知识---input输入块
对于如下的界面,介绍一种实现方式. 可以将整个界面分为三块,左中右.通过display: inline-block;和float: right;左右浮动效果实现. 代码如下: <!DOCTYPE ...
- Python学习进程(8)字符串內建函数
Python字符串內建函数实现了string模块的大部分方法,并包括了对Unicode编码方式的支持. (1)capitalize(): 将字符串的第一个字母变成大写,其他字母变小写. ...
- python中初始化实例属性
虽然我们可以自由地给一个实例绑定各种属性,但是,现实世界中,一种类型的实例应该拥有相同名字的属性.例如,Person类应该在创建的时候就拥有 name.gender 和 birth 属性,怎么办? 在 ...
- DNS 递归/迭代 原理
递归查询 递归:客户端只发一次请求,要求对方给出最终结果.一般客户机和服务器之间属递归查询,即当客户机向DNS服务器发出请求后,若DNS服务器本身不能解析,则会向另外的DNS服务器发出查询请求,得到结 ...
- php自带函数去除html标记
strip_tags 去掉 HTML 及 PHP 的标记. 语法: string strip_tags(string str); 传回值: 字串 函式种类: 资料处理 内容说明 本函式可去掉字串中包含 ...
- 如何去掉Intellij IDEA过多的警告 设置警告级别
Intellij IDEA的代码提示系统很强大,根据严格的代码规范,包括简洁程度,运行效率,潜在bug提前发现等等给你做出了除编译器之外的大量额外提示.但这些提示有时会给我们带来困扰,比如弄的界面很乱 ...
- Ubuntu或Linux搭建网站环境常见问题详解
本屌丝常见的问题已经全部记录如下,如大家有其他问题欢迎随时跟我进行交流. 1.无法进行软件源安装 提示信息:Package has no installation candidate 具体信息如下: ...
- 最长k可重区间集
P3358 最长k可重区间集问题 P3357 最长k可重线段集问题 P3356 火星探险问题 P4012 深海机器人问题 P3355 骑士共存问题 P2754 [CTSC1999]家园 题目描述 ...
- Solr新建collection时报错 Caused by: Direct buffer memory
错误如下 [root@192.168.1.235 conf]# curl "http://192.168.1.235:8983/solr/admin/collections ?action= ...