[LeetCode By Python] 3. Longest

2016-11-16  本文已影响172人  乐乐可爱睡觉

一、题目


Longest Substring Without Repeating Characters.png

二、解题


1)题意

2)关键点

三、思考:


四、尝试与结果

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        if (len(s) <= 1):
            return len(s)

        i = 0
        j = 1
        tempStr = s[0:1]
        templength = 1
        maxlength = 1

        while (True):
            c = s[j]
            if (c in tempStr):
                t = tempStr.find(c) + 1
                i = i + t #新的i的坐标

             #当下标为j时,s[i:j]这个字串是不包括j的,所以要把j+1(切片不会越界)
            tempStr = s[i:j+1] 
            templength = len(tempStr)

            if (templength > maxlength):
                maxlength = templength
            j = j + 1

            if (j >= len(s)):
                break

        return maxlength

结果:AC

上一篇下一篇

猜你喜欢

热点阅读