Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:
Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3:
Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints:
0 <= s.length <= 5 * 104 s consists of English letters, digits, symbols and spaces.
# 用右指針遍歷整個字符串 for right inrange(len(s)): # 當當前字符在字符集中時,移動左指針並從集合中移除字符,直到當前字符不在字符集中 while s[right] in char_set: # 移除最左邊的字符 char_set.remove(s[left]) # 左指針右移 left += 1 # 將當前字符加入集合 char_set.add(s[right]) # 更新最大長度 max_len = max(max_len, right - left + 1)