1. 最长不含重复字符的子字符串
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
方法一:
def lengthOfLongestSubstring(s):
n = len(s)
max_len = 0
start = 0
char_index = {}
for end in range(n):
if s[end] in char_index and char_index[s[end]] >= start:
start = char_index[s[end]] + 1
char_index[s[end]] = end
max_len = max(max_len, end - start + 1)
return max_len
方法二:
def lengthOfLongestSubstring(s):
max_len = 0
start = 0
seen = {}
for i, char in enumerate(s):
if char in seen and seen[char] >= start:
start = seen[char] + 1
seen[char] = i
max_len = max(max_len, i - start + 1)
return max_len
标签:end,max,Study,len,char,start,Algorithms,seen,Part27
From: https://blog.csdn.net/qq_24058289/article/details/142037062