leetcode之Longest Substring Without Repeating Characters

来源:互联网 发布:植物精灵for mac 编辑:程序博客网 时间:2024/05/16 02:21
再想了一下,发现重复算了好多次。应该可以在记录每个重复之后再开始算就对了。渣渣代码如下:
class Solution(object):    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        if len(s) < 2:            return len(s)        m = []        maxofamount = 0        for i in xrange(len(s)):            middle = ''            count = 0            for j in xrange(i, len(s)):                if s[j] not in middle:                    middle += s[j]                    count += 1                    if j == len(s) - 1:                        if count > maxofamount:                            return count                        else:                            return maxofamount                    if count >= 95:                        return 95                else:                    if count > maxofamount:                        maxofamount = count                    break        return maxofamount

0 0