-
leetcode2sumc-LeetCode-3.Longest_Substring_Without_Repeating_Characters下载
资源介绍
leetcode
2
和
c
LeetCode-3.Longest_Substring_Without_Repeating_Characters
给定一个字符串,找出没有重复字符的最长子字符串的长度。
示例
1:
输入:“abcabcbb”
输出:3
解释:答案是“abc”,长度为
3。
解决方案
Python3:类解决方案:
def
lengthOfLongestSubstring(self,
s):
dicts
=
{}
maxlength
=
start
=
0
for
i,value
in
enumerate(s):
#
"abcabcbb"
#
"bbbbb"
if
value
in
dicts:
sums
=
dicts[value]
+
1
if
sums
>
start:
start
=
sums
num
=
i
-
start
+
1
if
num
>
maxlength:
maxlength
=
num
dicts[value]
=
i
#
dicts
=
{a:1},
maxlength
=
1
#
dicts
=
{a:1,b:2},
maxlength
=
2