21 5 月 2020

leetcode第三题

原题https://leetcode.com/problems/longest-substring-without-repeating-characters/submissions/

经典题 最长子窜长度

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if s=="":
            return 0
        
        max1 = 1
        for i in range(0,len(s)-1):
            b=[]
            b.append(s[i])
            for j in range(i+1,len(s)) :
            
                if s[j] not in b:
                    b.append(s[j])
                else:
                    max1=(len(b) if len(b)>max1 else max1)#遇到重复立马打断循环 计数
                    break
                #print(b)
            max1=(len(b) if len(b)>max1 else max1)#没有遇到任何重复 计数
            
        
        
        return max1