1. 程式人生 > >領扣-無重復字符的最長子串-Python實現

領扣-無重復字符的最長子串-Python實現

test color 添加 bbbb 輸入 無重復 cab 實現 python實現

給定一個字符串,請你找出其中不含有重復字符的 最長子串 的長度。

示例 1:

輸入: "abcabcbb"
輸出: 3 
解釋: 因為無重復字符的最長子串是 "abc",所以其長度為 3。

示例 2:

輸入: "bbbbb"
輸出: 1
解釋: 因為無重復字符的最長子串是 "b",所以其長度為 1。

示例 3:

輸入: "pwwkew"
輸出: 3
解釋: 因為無重復字符的最長子串是 "wke",所以其長度為 3。
     請註意,你的答案必須是 子串 的長度,"pwke" 是一個子序列,不是子串。

s=pwwkew
max_number = 0
number = 0
test = ‘‘
for i in s: #如果i不在test字符串裏面,字符串test添加這個字符,number+1 if i not in test: test += i number += 1 global s1 s1=test else: #i在test字符串裏 if number >= max_number: max_number = number index = test.index(i) test = test[(index+1):] + i number
=len(test) if number > max_number: max_number = number print(s1) print(max_number)

領扣-無重復字符的最長子串-Python實現