diff --git a/002. Longest Substring Without Repeating Characters/README.md b/002. Longest Substring Without Repeating Characters/README.md index 224b0b8..2d12d03 100644 --- a/002. Longest Substring Without Repeating Characters/README.md +++ b/002. Longest Substring Without Repeating Characters/README.md @@ -1,9 +1,19 @@ +# 思路(C++) + +此题需要好好读懂题意:**没有重复字符**的最长子字符串的**长度**。 + +首先,求的只是长度,那么一定有一个 trace 来边记录边比较(max)。 +其次,没有重复字符几乎是唯一条件,那么检查重复显然用 k-v mapping. +最后,要考虑一次迭代过程中,如何度量这个长度。 + +---- + 设 substr 的起点为 start(s), 终点为 last(l). 每一次迭代,记录一张索引表。 abcabcbb ^ ^ s l - + |char|pos| |:--:|:-:| | a | 0 | @@ -25,7 +35,6 @@ cache[s[last]] = last; 注意最终还需要比较一次,返回 `max(ret, s.size() - start)` +## Python - - - +思路与 C++ 完全一致。 \ No newline at end of file diff --git a/002. Longest Substring Without Repeating Characters/solution.h b/002. Longest Substring Without Repeating Characters/solution.h index 6c463e6..a614b1e 100644 --- a/002. Longest Substring Without Repeating Characters/solution.h +++ b/002. Longest Substring Without Repeating Characters/solution.h @@ -8,16 +8,16 @@ using std::max; class Solution { public: int lengthOfLongestSubstring(string s) { - size_t ret=0, start=0; - unordered_map cache = {{s.front(), 0}}; - for (size_t last=1; last < s.size(); ++last) { - auto found = cache.find(s[last]); - if (found != cache.end() && found->second >= start) { - ret = max(ret, last - start); + size_t ret = 0, start = 0; + unordered_map trace; + for (size_t i = 0; i < s.size(); ++i) { + auto found = trace.find(s[i]); + if (found != trace.end() && found->second >= start) { + ret = max(ret, i - start); start = found->second + 1; } - cache[s[last]] = last; + trace[s[i]] = i; } - return max(ret, s.size()-start); + return max(ret, s.size() - start); } }; diff --git a/002. Longest Substring Without Repeating Characters/solution.py b/002. Longest Substring Without Repeating Characters/solution.py new file mode 100644 index 0000000..ee98a44 --- /dev/null +++ b/002. Longest Substring Without Repeating Characters/solution.py @@ -0,0 +1,24 @@ +#!python3 + + +class Solution: + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + rlen = 0 + start = 0 + trace = {} + for index, ch in enumerate(s): + if ch in trace and trace[ch] >= start: + rlen = max(rlen, index - start) + start = trace[ch] + 1 + trace[ch] = index + return max(rlen, len(s) - start) + + +if __name__ == "__main__": + print(Solution().lengthOfLongestSubstring("abcabcbb") == 3) + print(Solution().lengthOfLongestSubstring("abba") == 2) + print(Solution().lengthOfLongestSubstring("pwwkew") == 3) diff --git a/README.md b/README.md index acc608b..f181d0a 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,11 @@ LeetCode LeetCode solutions in C++ 11 and Python3. -|NO.|Title|Solution|Note|Difficulty| -|---|-----|--------|--------|----------| -|0|[Two Sum](https://leetcode.com/problems/two-sum)|[C++](000.%20Two%20Sum/solution.h) [Python](000.%20Two%20Sum/solution.py)|[Note](000.%20Two%20Sum)|Easy| -|1|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers)|[C++](001.%20Add%20Two%20Numbers/solution.h) [Python](001.%20Add%20Two%20Numbers/solution.py)|[Note](001.%20Add%20Two%20Numbers)|Medium| -|2|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters)|[C++](002.%20Longest%20Substring%20Without%20Repeating%20Characters/solution.h) [Python](002.%20Longest%20Substring%20Without%20Repeating%20Characters/solution.py)|[Note](002.%20Longest%20Substring%20Without%20Repeating%20Characters)|Medium| +|NO.|Title|Solution|Note|Difficulty|Tag| +|---|-----|--------|----|----------|---| +|0|[Two Sum](https://leetcode.com/problems/two-sum)|[C++](000.%20Two%20Sum/solution.h) [Python](000.%20Two%20Sum/solution.py)|[Note](000.%20Two%20Sum)|Easy|`Mapping`| +|1|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers)|[C++](001.%20Add%20Two%20Numbers/solution.h) [Python](001.%20Add%20Two%20Numbers/solution.py)|[Note](001.%20Add%20Two%20Numbers)|Medium|`Linked List`| +|2|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters)|[C++](002.%20Longest%20Substring%20Without%20Repeating%20Characters/solution.h) [Python](002.%20Longest%20Substring%20Without%20Repeating%20Characters/solution.py)|[Note](002.%20Longest%20Substring%20Without%20Repeating%20Characters)|Medium|`Mapping`| |3|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays)|[C++](003.%20Median%20of%20Two%20Sorted%20Arrays/solution.h) [Python](003.%20Median%20of%20Two%20Sorted%20Arrays/solution.py)|[Note](003.%20Median%20of%20Two%20Sorted%20Arrays)|Hard| |4|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring)|[C++](004.%20Longest%20Palindromic%20Substring/solution.h) [Python](004.%20Longest%20Palindromic%20Substring/solution.py)|[Note](004.%20Longest%20Palindromic%20Substring)|Medium| |5|[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion)|[C++](005.%20ZigZag%20Conversion/solution.h) [Python](005.%20ZigZag%20Conversion/solution.py)|[Note](005.%20ZigZag%20Conversion)|Medium|