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. AtcoderExaWizards 2019题解

    传送门 \(A\ Regular\ Triangle\) 咕咕 \(B\ Red\ or\ Blue\) 咕咕咕 \(C\ Snuke\ the\ Wizard\) 我可能脑子真的坏掉了-- 容易发现 ...

  2. 0基础浅谈反射型xss (1)

    0X1:在学习xss之前,先快速学习相关的HTML代码 1.  <input>标签 文本域用法: <input  type="text" /> Type的作 ...

  3. scrapy之 downloader middleware

    一. 功能说明 Downloader Middleware有三个核心的方法 process_request(request, spider) process_response(request, res ...

  4. Java初学者的学习路线建议

    java学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是我你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈 ...

  5. 通过API方式查看Azure Sign-ins记录

    经确认,目前Sign-ins功能在中国区还没有开通.也没有相关的预计开通时间.您可以通过如下链接随时关注China Azure的最新公告:https://www.azure.cn/zh-cn/what ...

  6. 51.RocketMQ 顺序消费

    大部分的员工早上的心情可能不会很好,因为这时想到还有很多事情要做,压力会大点,一般到下午4点左右,状态会是一天中最好的,因为这时大部分的工作做得差不多了,又快要下班了,当然也不是绝对.要注意记录各下属 ...

  7. Nodejs统计每秒记录日志数

    问题:线上的写日志操作非常频繁,想统计每秒写了多少行数据?假如没法送一个消息写一个日志,问题也就变成了,每秒发送多少消息了. 日志采用log4js书写,格式如下: [-- ::33.548] [INF ...

  8. 问题 K: 周期串plus

    问题 K: 周期串plus 时间限制: 1 Sec  内存限制: 128 MB提交: 682  解决: 237[提交] [状态] [命题人:外部导入] 题目描述 如果一个字符串可以由某个长度为k的字符 ...

  9. golang (5) ---工程管理

    1. go mod 添加本地package依赖 go mod 作为golang新的版本管理工具,减少了对GOPATH的依赖. 但是对本地文件的依赖的时候,提示 build server: cannot ...

  10. (转)调优 DB2 UDB v8.1 及其数据库的最佳实践

    原文:https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0404mcarthur/index.html 简介 性能是 ...