[LeetCode]Longest Substring Without Repeating Characters题解
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.
刚刚看到这道题的时候还是比较难受的,因为左思右想也想不到解法。
O(n^2)算法复杂度的程序会超时,所以需要想出一种O(n)的方法得到最大子字符串。
看了一下LeetCode的discuss上,O(n)的方法基本思路都是一样的。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
//创建一个数组记录这个字符上一次出现的位置,初始化为-1
vector<int> charaterLastOccur(256 , -1);
int len= 0, left = -1;//left是子字符串开始的位置
for(int i = 0; i < s.size(); i++){
//当前字符上一次出现的位置在left右边(>left)的时候,更新left的位置
if(charaterLastOccur[s[i]] > left){
left = charaterLastOccur[s[i]];
}
//否则的话,更新res
//新字符串长度(i-left)有可能比之前记录的len大,所以也更新一下
else{
len = max(len, i - left);
}
//一定记得在循环最后面加上这句,记录(更新)当前字符出线的位置
charaterLastOccur[s[i]] = i;
}
return len;
}
};
基本的思路都在注释里了,这里举个栗子巩固一下。
例如字符串“abcabcd“,当遍历完了前面的“abc”后,
1. 再遍历到“a”,因为a已经在left(=-1)后面出现过,所以left更新为a上次出现的位置0,此时从left后面到i就没有重复的字符啦(从left到i的字符串也是当前循环选定的符合条件的子字符串)
2. 再遍历到“b”,因为b已经在left(=0)后面出现过,所以left更新为b上次出线的位置1,
3. …
每次都有检查子字符串的长度并且把最大的保存下来,所以最后就得到了我们想要的无重复字符的最长子字符串的长度了。
[LeetCode]Longest Substring Without Repeating Characters题解的更多相关文章
- LeetCode longest substring without repeating characters 题解 Hash表
题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode——Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [Leetcode] Longest Substring Without Repeating Characters (C++)
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
随机推荐
- eclipse的classpath(build path)和classpaht几种设置的方式
1,默认eclipse有自己的classpath的路径并不是环境变量中配置的classpah. 2,eclipse的classpath每个项目不同,一般是在工作区的当前项目的class下. 2.1,可 ...
- 前后端分离——token超时刷新策略
前言 记录一下前后端分离下————token超时刷新策略! 需求场景 昨天发了一篇记录 前后端分离应用——用户信息传递 中介绍了token认证机制,跟几位群友讨论了下,有些同学有这么一个疑惑:toke ...
- python基础知识梳理----3基本数据类型,int,bool,str ,for 循环,迭代
一:python的基本类型 1.int -----整数,主要进行数学运算 2.str -----字符串,可以保存少量数据,并进行相关操作, 3. bool ---布尔类型,判断真假 4.list ...
- E - Guess the Root 拉格朗日差值法+交互
题目传送门 题意:告诉你存在一个未知项系数最高为10的$f(x)$,你最多可以有50次询问,每次询问给出一个$x'$,系统会返回你$f(x')$的值,你需要猜一个$x''$,使得$f(x'')=0$, ...
- FindLine把多行查找改为多行替换
Sub FindLine() Dim textSelection As TextSelection textSelection = DTE.ActiveDocument.Selection textS ...
- (转)LVS安装使用详解
原文:https://www.cnblogs.com/MacoLee/p/5856858.html 简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由 ...
- 【文档】一、Mysql Binlog概述
Binlog是一系列日志文件,他们包含的内容是Mysql数据内容的改变.如果想开启binlog功能,需要在启动时带上--log-bin参数. binlog是从Mysql3.23.14版本开始的.它包含 ...
- 【Qt开发】QTime类
QTime类为用户提供一系列时间的函数,封装的很全面,几乎满足了各种时间的需求. 常用方法介绍 1.QTime addMSecs(int ms) const 当前时间增加毫秒,ms可为负 2.QTim ...
- JavaScript设计模式-22.观察者模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- js解决千分符问题
js脚本function: //js数字千分符处理 function commafy(num) { num = num + ""; var re = /(-?\d+)(\d{3}) ...