#3 longest-substring-without-repeating-characters

来源:互联网 发布:快喵 mac 编辑:程序博客网 时间:2024/05/14 20:44

最长不重复子串:

The firt coding;
Using list

class Solution(object):    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        arr = []        n = 0        for str in s :            if(str in arr) :                if(len(arr)>n) :                    n = len(arr)                arr = arr[arr.index(str)+1:]            arr.append(str)        if(len(arr) > n):            n = len(arr)        return n

Runtime: 156 ms

The Second coding :
Using dict {}

class Solution(object):    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        arr = {}        n,i,j = 0,0,0        for str in s :            if(str in arr) :                n = max(j-i,n)                i = max(arr[str], i)            j+=1            arr[str] = j        n = max(j-i,n)        return n

Runtime: 116 ms

1 0
原创粉丝点击