#3 Solution 1 [Best SE]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if s == '': return 0 if len(set(s)) == 1: return 1 h = 0 t = 1 n = 1 m = 1 l = len(s) while True: if s[t] in s[h:t]: n = t - h h = s[h:t].index(s[t])+h+1 t = h + 1 m = max(m, n) n = 1 else: t += 1 n += 1 if t == l: break m = max(m, n) return m |
[……]阅读全文