【python学习记录3】Longest Substring W

2016-08-30  本文已影响22人  hitsunbo

问题描述

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

详细问题

代码实现

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        lens = len(s)
        if lens < 2 :
            return lens
        maxlen = 0
        p = 0
        q = 1
        while q < lens :
            if p == q :
                q = q+1
                continue
            if s[q] in s[p:q] :
                p = p + 1
            else :
                q = q + 1
                if (q - p) > maxlen :
                    maxlen = q - p
        if (q - p) > maxlen :
            return q - p
        else :
            return maxlen

经验总结

上一篇 下一篇

猜你喜欢

热点阅读