題目:
給定一個字符串,請你找出其中不含有重複字符的 最長子串 的長度。
示例 1:
輸入: "abcabcbb" 輸出: 3 解釋: 因爲無重複字符的最長子串是 "abc",所以其長度爲 3。
示例 2:
輸入: "bbbbb" 輸出: 1 解釋: 因爲無重複字符的最長子串是 "b",所以其長度爲 1。
示例 3:
輸入: "pwwkew" 輸出: 3 解釋: 因爲無重複字符的最長子串是 "wke",所以其長度爲 3。 請注意,你的答案必須是 子串 的長度,"pwke"是一個子序列,不是子串。
解:
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
length = len(s)
result=[]
if s == "":
return 0
elif s.strip() == "":
return 1
for i in range(length):
temp = s[i]
for j in range(i+1,length):
if s[j] not in temp:
temp = temp + s[j]
else:
break
result.append(temp)
result.sort(lambda x,y:cmp(len(x), len(y)))
return len(result[-1])