原题
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
题干
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的
这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的这是占字数的
解
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s:
return 0
end = 0
start = 0
res = 0
lookup = set()
while start < len(s) and end < len(s):
if s[end] not in lookup:
lookup.add(s[end])
res = max(res, end - start+1)
end += 1
else:
lookup.discard(s[start])
start += 1
return res
解
# 还有更好的解法 不过我没想到