首页 > 其他 > 详细

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

时间:2019-06-02 19:52:09      阅读:82      评论:0      收藏:0      [点我收藏+]

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

  

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

原文:https://www.cnblogs.com/rosyYY/p/10963984.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!