無重復字符的最長子串

給定一個字符串 s ,請你找出其中不含有重復字符的 最長子串 的長度。

  • 主要思想是滑窗并記錄最大值
  • 途徑:使用字典記錄出現(xiàn)的次數(shù)
  • 踩坑注意:除了新創(chuàng)建key以外,還有以前有過這個字符,但是又去掉的情況,即dict_[s[ed]]==0。
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        st = 0
        ed = 1
        maxl = 1
        if len(s) == 0:
            return 0
        if len(s) == 1:
            return maxl
        dict_ = {}
        dict_[s[st]]=1
        while ed < len(s):
            if s[ed] not in dict_ or dict_[s[ed]]==0:
                dict_[s[ed]]=1
                ed+=1
                continue
            if dict_[s[ed]]>0:
                dict_[s[ed]]+=1
                maxl = max(maxl,ed-st)
            while dict_[s[ed]]>1:
                dict_[s[st]]-=1
                st = st+1
            ed = ed+1
        maxl = max(maxl,ed-st)
        return maxl
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容