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题解的更多相关文章

  1. LeetCode longest substring without repeating characters 题解 Hash表

    题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  4. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  5. C++ leetcode Longest Substring Without Repeating Characters

    要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  7. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  8. LeetCode——Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. [Leetcode] Longest Substring Without Repeating Characters (C++)

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

随机推荐

  1. guzzle 简单使用记录

    用 guzzle 发送一个包含指定请求头,请求体的 post 请求,并获取指定内容: <?php include_once "guzzle.phar"; use Guzzle ...

  2. BZOJ3729: Gty的游戏(伪ETT)

    题面 传送门 前置芝士 巴什博奕 \(Nim\)游戏的改版,我们现在每次最多只能取走\(k\)个石子,那么\(SG\)函数很容易写出来 \[SG(x)=mex_{i=1}^{\min(x,k)}SG( ...

  3. express 直接返回HTML文件

    一般情况下用的是模板引擎,如jade: res.render('detail',{ // 使用render() #http://www.expressjs.com.cn/4x/api.html#res ...

  4. 字符串模式匹配算法1 - BF和KMP算法

    在字符串S中定位/查找某个子字符串P的操作,通常称为字符串的模式匹配,其中P称为模式串.模式匹配有多种算法,这里先总结一下BF算法和KMP算法. 注意:本文在讨论字符位置/指针/下标时,全部使用C语法 ...

  5. springboot 使用itextpdf 框架实现多个图片合成一个pdf文件

    以下两个方法引入头 import com.lowagie.text.*; import com.lowagie.text.pdf.PdfWriter; import org.apache.pdfbox ...

  6. 移动端<meta>属性配置讲解(整理)

    meta标签,是head区的辅助标签 HTML代码如下: <meta charset="utf-8"><meta http-equiv="X-UA-Co ...

  7. Linux和Windows数据同步

    正在做一个小项目,关于Linux和Windows数据同步,新知识很兴奋,比赛很有信心,加油吧少年 项目进行中:今晚实验室包宿开整.

  8. js 封装trim()方法,去掉空格

    <script> //定义一个对象 - 名字是$ var $$ = function() {}; //原型 $$.prototype = { $id:function(id) { retu ...

  9. 使用scp命令,远程上传下载文件/文件夹

    1.从服务器下载文件 scp username@servername:/path/filename /local/path例如: scp ubuntu@117.50.20.56:/ygf/data/d ...

  10. 认识CSS中标题引入icon图标

    前端之HTML,CSS(十一) icon图标 icon图标的使用 获取网站的中标题icon图标,以京东为例:在域名后添加/favicon.ico Enter打开 鼠标右键,图标另存为下载icon图标, ...