Skip to content

Commit

Permalink
added python solution for 002
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 18, 2017
1 parent 4a2518c commit 1df8968
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
17 changes: 13 additions & 4 deletions 002. Longest Substring Without Repeating Characters/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# 思路(C++)

此题需要好好读懂题意:**没有重复字符**的最长子字符串的**长度**

首先,求的只是长度,那么一定有一个 trace 来边记录边比较(max)。
其次,没有重复字符几乎是唯一条件,那么检查重复显然用 k-v mapping.
最后,要考虑一次迭代过程中,如何度量这个长度。

----

设 substr 的起点为 start(s), 终点为 last(l). 每一次迭代,记录一张索引表。

abcabcbb
^ ^
s l

|char|pos|
|:--:|:-:|
| a | 0 |
Expand All @@ -25,7 +35,6 @@ cache[s[last]] = last;

注意最终还需要比较一次,返回 `max(ret, s.size() - start)`

## Python




思路与 C++ 完全一致。
16 changes: 8 additions & 8 deletions 002. Longest Substring Without Repeating Characters/solution.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ using std::max;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
size_t ret=0, start=0;
unordered_map<char, size_t> 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<char, size_t> 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);
}
};
24 changes: 24 additions & 0 deletions 002. Longest Substring Without Repeating Characters/solution.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 1df8968

Please sign in to comment.