图片由网上提供仅供参考:
1.本地测试
def lengthOfLongestSubstring(s):
max_len = 0
if s is None or len(s) == 0:
print(max_len)
str_dict = {}
one_max = 0
start = 0
for i in range(len(s)):
if s[i] in str_dict and str_dict[s[i]] >= start:
start = str_dict[s[i]] + 1
one_max = i - start + 1
str_dict[s[i]] = i
max_len = max(max_len, one_max)
print(max_len)
if __name__ == '__main__':
s="abcabcbb"
lengthOfLongestSubstring(s)
2.手动输入:
import sys def lengthOfLongestSubstring(s): max_len = 0 if s is None or len(s) == 0: return max_len str_dict = {} one_max = 0 start = 0 for i in range(len(s)): if s[i] in str_dict and str_dict[s[i]] >= start: start = str_dict[s[i]] + 1 one_max = i - start + 1 str_dict[s[i]] = i max_len = max(max_len, one_max) return max_len if __name__ == '__main__': s = sys.stdin.readline().strip() print(lengthOfLongestSubstring(s))