Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02576af

Browse files
committed
add 3 sliding window
1 parent a2acf39 commit 02576af

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

‎LeetCodeSolutions/003.py‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,32 @@
1212

1313
class Solution:
1414
def lengthOfLongestSubstring(self, s):
15-
i, cnt = 0, 0
16-
HashMap = {}
17-
for idx, val in enumerate(s):
18-
if HashMap.get(val): i = max(HashMap.get(val), i)
19-
HashMap[val] = idx + 1
20-
cnt = max(cnt, idx-i+1)
21-
return cnt
15+
# i, cnt = 0, 0
16+
# HashMap = {}
17+
# for idx, val in enumerate(s):
18+
# if HashMap.get(val): i = max(HashMap.get(val), i)
19+
# HashMap[val] = idx + 1
20+
# cnt = max(cnt, idx-i+1)
21+
# return cnt
2222

23-
s = 'abcabcbb'
24-
s = 'pwwkew'
25-
s = 'ewwkea'
23+
res = left = right = 0
24+
hashMap = {}
25+
while right < len(s):
26+
if s[right] in hashMap:
27+
res = max(res, right-left)
28+
left = max(left, hashMap[s[right]])
29+
hashMap[s[right]] = right+1
30+
right += 1
31+
res = max(res, right-left)
32+
return res
33+
34+
s = "abcabcbb"
35+
# s = "bbbbb"
36+
# s = "pwwkew"
37+
# s = ""
38+
# s = " "
39+
# s = "ab"
40+
# s = "abba"
2641

2742
mat = Solution()
2843
mat.lengthOfLongestSubstring(s)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /