-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path3.Longest-Substring-Without-Repeating-Characters.py
63 lines (48 loc) · 1.4 KB
/
3.Longest-Substring-Without-Repeating-Characters.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# https://leetcode.com/problems/longest-substring-without-repeating-characters/
#
# algorithms
# Medium (25.7%)
# Total Accepted: 685,233
# Total Submissions: 2,668,623
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
length = len(s)
if length < 2:
return length
hash_map = {}
head, tail = 0, 0
res = 0
while tail < length:
if hash_map.get(s[tail], -1) >= head:
head = hash_map[s[tail]] + 1
hash_map[s[tail]] = tail
res = max(res, tail - head + 1)
tail += 1
return res
# 二刷的时候速度更快的做法
class Solution1(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
length = len(s)
res = float('-inf')
hash_map = {}
pre_idx = 0
for idx in xrange(length):
ch = s[idx]
if ch in hash_map:
res = max(res, idx - pre_idx)
ch_pre_idx = hash_map[ch]
for i in xrange(pre_idx, ch_pre_idx + 1):
if hash_map[s[i]] < ch_pre_idx:
del hash_map[s[i]]
pre_idx = ch_pre_idx + 1
hash_map[ch] = idx
res = max(res, length - pre_idx)
return res