【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
2014-12-23 13:40
302 查看
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
双指针begin,end.扩展end,收缩begin.
[begin,end]之间为无重复字母的子串。
可与Longest Substring with At Most Two Distinct Characters对照看。
解法一:
使用unordered_map记录每个字母最近出现下标。
[begin, end]表示无重复字符的窗口。
在end指向一个窗口中的字符时,需要将begin收缩到该字符之后,以免与end重复。
时间复杂度:O(n)
空间复杂度:O(n)
![](http://images.cnitblog.com/blog/458814/201501/292231072538708.jpg)
解法二:
使用record[128]记录A~Z,a~z在窗口中的出现次数。
由于无重复的要求,因此在end指向一个已经出现过的字母(即record[s[end]]不为零)
需要收缩begin直到begin遇到end或者record[s[end]]为零。
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
双指针begin,end.扩展end,收缩begin.
[begin,end]之间为无重复字母的子串。
可与Longest Substring with At Most Two Distinct Characters对照看。
解法一:
使用unordered_map记录每个字母最近出现下标。
[begin, end]表示无重复字符的窗口。
在end指向一个窗口中的字符时,需要将begin收缩到该字符之后,以免与end重复。
时间复杂度:O(n)
空间复杂度:O(n)
class Solution { public: int lengthOfLongestSubstring(string s) { if(s == "") return 0; unordered_map<char, int> m; //char-index map int ret = 1; int begin = 0; m[s[begin]] = 0; int end = 1; while(end < s.size()) { //extend if(m.find(s[end]) == m.end() || m[s[end]] < begin) { ; } //shrink else { begin = m[s[end]]+1; } ret = max(end-begin+1, ret); m[s[end]] = end; end ++; } return ret; } };
![](http://images.cnitblog.com/blog/458814/201501/292231072538708.jpg)
解法二:
使用record[128]记录A~Z,a~z在窗口中的出现次数。
由于无重复的要求,因此在end指向一个已经出现过的字母(即record[s[end]]不为零)
需要收缩begin直到begin遇到end或者record[s[end]]为零。
class Solution { public: int lengthOfLongestSubstring(string s) { int longest = 0; int begin = 0; int end = 0; int record[128] = {0}; //initial to all 0 while(end < s.size()) { while(record[s[end]]>0 && begin<end) { record[s[begin]] --; begin ++; } //record[s[end]]==0 || begin==end record[s[end]] ++; longest = max(longest, end-begin+1); end ++; } return longest; } };
![](http://images.cnitblog.com/blog/458814/201502/191606432835716.jpg)
相关文章推荐
- Leetcode-- Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters (3)
- LeetCode(3) Longest Substring Without Repeating Characters
- [leetcode Q3] —— Longest Substring Without Repeating Characters
- 【leetcode】3. Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)
- leetcode3. Longest Substring Without Repeating Characters
- [LeetCode 3] Longest Substring Without Repeating Characters
- [leetcode]Longest Substring Without Repeating Characters
- LeetCode3. Longest Substring Without Repeating Characters
- [LeetCode] 3. Longest Substring Without Repeating Characters
- leetcode substring without repeating characters
- leetcode之 Longest Substring Without Repeating Characters
- leetcode挨个儿刷150429(5):Longest Substring Without Repeating Characters
- 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)
- 003 Longest Substring Without Repeating Characters [Leetcode]
- Java Leetcode Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters题解