-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path003LongestSubstringWithoutRepeatingCharacters.py
More file actions
36 lines (35 loc) · 1.16 KB
/
003LongestSubstringWithoutRepeatingCharacters.py
File metadata and controls
36 lines (35 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python3
class Solution(object):
def lengthOfLongestSubstring(self,s):
"""
:type s: str
:rtype: int
"""
max_count = 0
max_substr = []
substr = []
count = 0
for x in s:
if x in substr:
# 有重复,复位
substr = substr[substr.index(x)+1:]
print('截取后:', substr)
substr.append(x)
count = len(substr)
else:
# 没有重复,加1
count = count + 1
substr.append(x)
if count > max_count:
max_count = count
max_substr = substr[:]
print(count,substr)
return max_count, max_substr
if __name__ == '__main__':
max_count, max_substr = Solution().lengthOfLongestSubstring("abcabcbb")
assert max_count == 3
max_count, max_substr = Solution().lengthOfLongestSubstring("ohvhjdml")
assert max_count == 6
max_count, max_substr = Solution().lengthOfLongestSubstring("ggububgvfk")
assert max_count == 6
print('最大长度:',max_count," 最大子串:",max_substr)