只要注意点最长非重复字串出现在字符串末尾的情况就好,挺简单的。(时间和内存都惨不忍睹)
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: string = s #--- #lenth = 1 if string == "": return 0 if len(string) == 1: return 1 # # --- # find substring longestSubstring = 0 for start in range(len(string)): for end in range(start +1 , len(string)): if string[end] not in string[start:end]: # find whether current letter duplication #--- #check if there have letter after end #careful with the letter string[end] if end +1 == len(string): if (end - start +1) > longestSubstring: return (end - start +1) else: return longestSubstring else: if len(string[start:end]) > longestSubstring: longestSubstring = len(string[start:end]) break return longestSubstring