LeetCode03--无重复字符的最长子串

 1 '''
 2 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
 3 示例 1:
 4 输入: "abcabcbb"
 5 输出: 3
 6 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
 7 '''
 8 
 9 
10 class Solution:
11     def lengthOfLongestSubstring(self, s):
12         """
13         :type s: str
14         :rtype: int
15         """
16         if s:
17             maxStr = s[0]
18             maxLen = 1
19             resultLen = 0
20             for t in s[1:]:
21                 if t in maxStr:
22                     tempLen = len(maxStr)
23                     if tempLen > maxLen:
24                         maxLen = tempLen
25                     maxStr += t
26                     maxStr = maxStr[maxStr.index(t) + 1:]
27                 else:
28                     maxStr += t
29                     resultLen = len(maxStr)
30             # print(maxLen)
31             # print(resultLen)
32             return max(maxLen, resultLen)
33         else:
34             return 0
35 
36 
37 if __name__ == '__main__':
38     s = 'erxdcdactfvgcfcfxdrw'
39     max = Solution().lengthOfLongestSubstring(s)
40     print(max)

 

posted @ 2018-12-03 14:30  浅尝辄止易初心不改难  Views(118)  Comments(0Edit  收藏  举报