學習筆記 | 無重複字符最長子串

01 無重複字符最長子串

  • 快慢指針
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        ans = 0
        if not s:
            return ans
        
        i = 0
        j = 0
        dic = {}
        
        while j < len(s):
            if s[j] in dic:
                i = max(dic[s[j]]+1,i)
            
            dic[s[j]] = j
            ans = max(ans,j-i+1)
            
            j += 1
        return ans
            
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章