[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 ...
随机推荐
- robot framework-tags(标签)实例
robot framework的标签是一个简单而又强大的分类机制,功能如下: 标签在reports,logs以及测试数据中展示,显示关于测试用例的元数据信息 用例的执行统计(total,passed, ...
- jmeter+ant+jenkins+mac报告优化(一):解决Min Time和Max Time显示NaN
一.在上篇博客中生成的报告有两个问题: 1.date not defined 2.Min Time和Max Time显示成了NaN 二.Jmeter+Ant报告生成原理: 1.在Jmeter的extr ...
- [iOS笔试600题]二、常识篇(共有72题)
[B]1.NSObject是一个根类,几乎所有的类都是从它派生而来.但是根类并不拥有真它类都有的alloc和init方法?[判断题] A. 正确 B. 错误 [A]2. UIResponder可以让继 ...
- js及Java中对于两个时间日期的判断脚本
JS脚本: function checkDateIsEdited(createDate) { var compareDate = createDate.replace("-",&q ...
- find查找文件命令 - Linux系统中的常用技巧整理
“find”在Linux系统中是比较常用的文件查找命令,使用方法有很多,可以拥有查找文件.文件目录.文件更新时间.文件大小.文件权限及对比文件时间.下面是整理的“find”常用方法,方便以后需要的时候 ...
- bzoj4998: 星球联盟(link-cut-tree)
题面 bzoj 题解 bzoj2959: 长跑的弱化版 产生了环就并查集维护一下 Code #include<bits/stdc++.h> #define LL long long #de ...
- Python 求“元组、列表、字典、数组和矩阵”的大小
总结: 首先 import numpy as np A = np.random.randint(1,100,size = (4,5)) >>A>>array([[56, 96, ...
- 关于开发环境无法运行applet
测试一下IE java vm 如果没有正确显示出来,说明java vm插件没有装好: 我用Uninstalle 来清理注册表:重装jdk 1.6_45
- c# Equals方法
很多C#的教材都会强调对象相等的概念.我们都知道,在C#的世界里存在两种等同性.一种是逻辑等同性:如果两个对象在逻辑上代表同样的值,则称他们具有逻辑等同性.另一种是引用等同性:如果两个引用指向同一个对 ...
- 那些H5用到的技术(6)——数字滚动特效
前言原理源码使用方式补充CountUp.js 前言 会有这么一种情况,H5页面需要进行数字统计展示,以此来强调产品or工作的成果.如果只是静态显示一个数字,总是感觉生硬.对比如下: 是不是瞬间高大上了 ...