[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 ...
随机推荐
- PHP 函数功能参考
basename() 返回路径中的文件名部分 chgrp() 改变文件组 chmod() 改变文件模式 chown() 改变文件所有者 clearstatcache() 清除文件状态缓存 copy() ...
- 喝最烈的酒、挖最大的DONG——工具与技巧篇
本文作者:i春秋签约作家——黑色镰刀 0×00 前言 在这个科技发达的时代,很多时候工具都可以代替人做很多事情,之前我就有谈起过有企业将人工智能运用于网络安全方面,像如今,也有更多更人性化更智能的工具 ...
- 爬虫5:beautifulsoup
灵活方便的网页解析库,处理高效,支持多种解析器,利用它不用编写正则表达式即可方便的实现网页信息的提取 一. BeautifulSoup的几种解析库 解析器 使用方法 优势 劣势 Pyt ...
- ps与grep组合命令使用
管道命令 我们在做运维的时候,经常会使用这个命令ps -ef | grep nginx. ps -ef 表示显示所有进程的消息. | 是管道命令.通常需要借助管道命令”|”多个命令的组合,形式如下: ...
- Java操作数据库实现"增删改查"
本文主要讲解JDBC操作数据库 主要实现对MySql数据库的"增删改查" 综合概述: JDBC的常用类和接口 一 DriverManager类 DriverManage类 ...
- ThreadLocal系列(三)-TransmittableThreadLocal的使用及原理解析
ThreadLocal系列(三)-TransmittableThreadLocal的使用及原理解析 上一篇:ThreadLocal系列(二)-InheritableThreadLocal的使用及原理解 ...
- idea 验证码
N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...
- C语言warning的收集和总结
1. warning: suggest parentheses around comparison in operand of '&' 分析: &运算符的优先级较低,低于==和!=运算 ...
- 运行零币Zcash(ZEC)
1.在基于 Ubuntu 或者 Debian 的系统中: $ sudo apt-get install \build-essential pkg-config libc6-dev m4 g++-mul ...
- Git学习系列之Git基本操作推送项目(图文详解)
前面博客 Git学习系列之Git基本操作提交项目(图文详解) 如果完成到一定程度,那么可以推送到远端在线仓库. 推送之前,请确保你已经设置了全局的 user.name 和 user.email, 如果 ...