leetcode-mid-array-3 Longest Substring Without Repeating Characters

mycode  99.21%

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        maxlen = 0
        temp =''
        for i in s:
            if i not in temp:
                temp += i
            else:
                maxlen = max(maxlen,len(temp))
                temp = temp[temp.index(i)+1:] + i
        maxlen = max(maxlen,len(temp))
        return maxlen

NOTE: This embodiment may be below the maximum length of a timely replacement

def lengthOfLongestSubstring(s):
        l = 0
        ls = ""
        for i in s:
            if i in ls:
                ls = ls[ls.index(i)+1:]
            ls += i
            if len(ls) > l:
                #maxres = max(maxres,len(ls))
                l = len(ls)
        return l
lengthOfLongestSubstring("abcdvdf")

  

Guess you like

Origin www.cnblogs.com/rosyYY/p/10963984.html