[Leetcode]3. Longest Substring Without Repeating Characters
来源:互联网 发布:怎样增加淘宝销量 编辑:程序博客网 时间:2024/05/21 22:29
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.
这个题目想了挺久的,最后参考了别人的解法。用两个指针,一个指向当前子串的头,一个指向尾,尾指针不断往后扫描,当有字符前面出现过了,记录当前子串长度和最优解的比较结果。然后头指针不断往后扫描,直到扫描到一个字符和尾指针相同,则尾指针继续扫描,当尾指针到达字符串结尾,算法结束。
class Solution {public: int lengthOfLongestSubstring(string s) { bool exist[256] = {false}; int maxLen = 0; int i = 0, j = 0; int n = s.size(); while (j < n) { if (exist[s[j]]) { maxLen = max(maxLen, j - i); for (;s[i] != s[j]; i++) exist[s[i]] = false; i++; j++; } else { exist[s[j]] = true; j++; } } maxLen = max(maxLen, n - i); return maxLen; }};
0 0
- [LeetCode]3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode --- 3. Longest Substring Without Repeating Characters
- [Leetcode] 3. Longest Substring Without Repeating Characters
- [leetcode] 3.Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- [Leetcode]3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- 【Leetcode】3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- LeetCode-3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode - 3.Longest Substring Without Repeating Characters
- Leetcode 3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters LeetCode
- (Leetcode)3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- LeetCode-3. Longest Substring Without Repeating Characters
- java反射机制
- 工厂模式学习
- 待补充:java类里的私有变量问题,在对象A里有一个对象B,调用对象B的方法获取变量,会有短暂的缓存
- 12306火车票查询
- 新浪实时股票数据接口http://hq.sinajs.cn/list=code
- [Leetcode]3. Longest Substring Without Repeating Characters
- SAX解析xml文件
- jacob操作office分享
- 读取手机相册,从手机相册读取图片剪切,拍照获取图片,获取拍照图片进行剪切
- 编程之美--小飞的电梯调度算法
- 设计模式之装饰器模式(C++)
- Oracle-rollup()函数
- C++中的可变参数模板
- WIONDWS 端口占用