LeedCode Longest Substring Without Repeating Characters
来源:互联网 发布:伦敦生活 知乎 编辑:程序博客网 时间:2024/06/10 20:22
网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
题目:
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 asubstring, "pwke"
is a subsequence and not a substring.
中文:
找给定字符串中最大的不同字符的子串
给定"abcabcbb"
的答案是"abc"
,长度是3。
给定"bbbbb"
的答案是"b"
,长度为1。
给定"pwwkew"
的答案是"wke"
,长度为3.请注意,答案必须是子字符串, "pwke"
是子序列,而不是子字符串
答案参考:http://blog.csdn.net/feliciafay/article/details/16895637#insertcode
思路:
使用i和j两个指针进行搜索,i代表候选的最长子串的开头,j代表候选的最长子串的结尾。
先假设i不动,那么在理想的情况下,我们希望可以一直右移j,直到j到达原字符串的结尾,此时j-i就构成了一个候选的最长子串。每次都维护一个max_length,就可以选出最长子串了。
实际情况是,不一定可以一直右移j,如果字符j已经重复出现过(假设在位置k),就需要停止右移了。记录当前的候选子串并和max_length做比较。接下来为下一次搜寻做准备。
在下一次搜寻中,i应该更新到k+1。这句话的意思是,用这个例子来理解,abcdef是个不重复的子串,abcdefc中(为了方便记录为abc1defc2),c1和c2重复了。那么下一次搜寻,应该跨过出现重复的地方进行,否则找出来的候选串依然有重复字符,且长度还不如上次的搜索。所以下一次搜索,直接从c1的下一个字符d开始进行,也就是说,下一次搜寻中,i应该更新到k+1。
代码:
class Solution {public: int lengthOfLongestSubstring(string s) { int n = s.length(); int i = 0, j = 0; //i是候选字符串的起点, j是候选字符串的终点。 int max_length = 0; int cur_length = 0; bool exist[256] = { false }; while (j < n) { if (!exist[s[j]]) { exist[s[j]] = true; //遍历过,记录为true j++; } else { while(s[i]!= s[j]) { exist[s[i]] = false; //新候选字串从第一个重复字符(当s[i] == s[j]时候的i)的后一位开始算,之前的i不算,等效于没有被扫描到,所以设为false. i++; } i++; j++; } cur_length = j - i; max_length = max_length > cur_length ? max_length : cur_length; } return max_length; } };
- LeedCode(3) - Longest Substring Without Repeating Characters
- LeedCode Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- 什么是-持续集成
- 在window上提交spark代码到远程测试环境上运行
- CTU Open Contest 2016 Suspicious Samples(单调队列)
- Linux自定义命令别名配置
- 有树形依赖的背包问题
- LeedCode Longest Substring Without Repeating Characters
- 认识AS中的Gradle
- 51nod 1183 编辑距离
- Mybatis Cause: java.lang.ClassNotFoundException: Cannot find class: BaseResultMap
- 强大的vim配置文件,让编程更随意
- TextView两端对齐
- springcloud_7(配置中心的高可用)
- hdu 3605 Escape (二分图匹配)
- hdu1166:敌兵布阵