3.Longest Substring Without Repeating Characters-python

来源:互联网 发布:java技术总监岗位职责 编辑:程序博客网 时间:2024/06/15 22:53

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 i<length:            ind = substr.find(s[i])            if 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
阅读全文
2 0
原创粉丝点击