Leetcode题解 - 3. Longest Substring Without Repeating Characters
来源:互联网 发布:单用户商城源码 编辑:程序博客网 时间:2024/05/23 01:12
题目
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.
思路
1. 使用一个临时字符串存放子串T
2. 每次从输入中获取一个字符C,并检查已有的子串T中是否已经存在该字符
- 如果存在,删除该字符之前的所有字符和它自己
3. 将C追加在子串T的尾部
4. 获取子串T的长度,并决定是否要更新最大值MAX
5. 重复1~4步骤,直到输入的所有的字符都处理完
6. 返回最大值MAX
本人的解
根据上述思路,代码如下。耗时29毫秒。
class Solution {public:/* 29 ms, Your runtime beats 51.30% of cpp submissions */int lengthOfLongestSubstring(string s) {int max = 0;string t("");for( int i = 0; i < s.length(); i++ ) {size_t pos = t.find(s[i], 0);if( pos != string::npos ) t.erase(0, pos + 1);t.append(1, s[i]);if( t.length() > max ) max = t.length();}return max;}};
改进版。
由于题目要求返回长度,而不是子字符串。所以可以使用string::back()函数取最后一个字符,处理完后使用string::pop_back()删除最后一个字符。
改进后,耗时12毫秒,算是比较快的了。
class Solution {public:/* 12 ms, Your runtime beats 87.44% of cpp submissions */int lengthOfLongestSubstring(string s) {int max = 0;string t("");while( s.length() ) {char a = s.back();size_t pos = t.find(a, 0);if( pos != string::npos ) t.erase(0, pos + 1);t.append(1, a);if( t.length() > max ) max = t.length();s.pop_back();}return max;}};
0 0
- LeetCode题解:Longest Substring Without Repeating Characters
- LeetCode题解:Longest Substring Without Repeating Characters
- Leetcode题解:Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters题解
- LeetCode题解--3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters : LeetCode 题解
- Leetcode题解(Python): 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 Algorithms 3. Longest Substring Without Repeating Characters 题解
- leetcode题解-3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters LeetCode题解
- LeetCode题解(Week 16):3. Longest Substring Without Repeating Characters
- LeetCode 3. Longest Substring Without Repeating Characters题解python
- Leetcode题解-3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters题解
- [LeetCode]3.Longest Substring Without Repeating Characters
- Git简介与安装
- 石油专业图件库(综合录井图、实时曲线图、井身结构图、井眼轨迹图、井口装置图等)
- 我的2016总结
- 三星公布Galaxy Note7燃损原因 再次向消费者的支持表示感谢
- 16年回想随笔
- Leetcode题解 - 3. Longest Substring Without Repeating Characters
- jquery中的stopPropagation
- 【图的实现】“广度优先搜索遍历”以及“深度优先搜索遍历”必会
- Linux和Windows学习日记2
- PAT (Advanced Level) 1006. Sign In and Sign Out (25) 解题报告
- 和为S的两个数字
- vim 使用技巧 set paste 解决粘贴乱序问题
- javaweb基于spring MVC的上传下载实例(下)
- poj_3280 Cheapest Palindrome(区间dp)