3.Longest Substring Without Repeating Characters-python

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.

Code

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        max=0
        curmax=0
        substr="" #当前的子字符串
        i=0
        length = len(s)
        while iif ind==-1:#no containing item
                substr+=s[i]
                curmax+=1
            else:
                if max < curmax:
                    max = curmax
                i = i - len(substr) + ind #回溯
                substr=""
                curmax = 0
            i+=1
        if max < curmax:
            max = curmax
        return max

你可能感兴趣的:(算法/LeetCode,经典算法,LeetCode题目研究)