Leetcode【3】 无重复字符的最长子串
给定一个字符串,找出不含有重复字符的最长子串的长度。
示例:
给定 "abcabcbb" ,无重复字符的最长子串是 "abc" ,所以其长度为3。
给定 "bbbbb" ,无重复字符的最长子串就是 "b" ,长度是1。
给定 "pwwkew" ,无重复字符的最长子串就是 "wke" ,长度是3。请注意答案必须是一个子串,"pwke" 是 子序列 而不是子串。
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: temp='' res='' for item in s: #对于字符串s中的每个字符 if item not in temp: #如果这个字符不在temp当中 temp += item if len(temp) > len(res): res = temp else: #如果这个字符在temp中 i = temp.index(item) #找到这个字符在temp中的索引 if i == len(temp)-1: #达到末尾 temp = item else: temp = temp[i+1:] + item if len(temp) > len(res): res = temp return len(res)
参考博主 https://blog.csdn.net/ma412410029/article/details/80814786

浙公网安备 33010602011771号