zoukankan      html  css  js  c++  java
  • 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

    注意:下面这种方式也可以及时更替最大长度

    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")
    

      

  • 相关阅读:
    053-1
    多项式ADT笔记(数据结构c版)
    052-188
    052-187
    052-186
    052-185
    052-184
    052-183
    052-182
    JS中的垃圾回收(GC)
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10963984.html
Copyright © 2011-2022 走看看