6.29 - hard - 18

76. Minimum Window Substring
这题用动态窗口一次性AC了,但是代码写的比较烂,有点虚,再去看看答案,看看别人怎么写的

class Solution(object):
   def minWindow(self, s, t):
       """
       :type s: str
       :type t: str
       :rtype: str
       """
       h = {}
       for c in t:
           h[c] = h.get(c, 0) + 1
       
       start = 0
       end = 0
       window = ""
       length = sys.maxint
       i = 0
       while i < len(s):
           if s[i] in h:
               h[s[i]] -= 1
               if all([v <= 0 for v in h.values()]):
                   if end - start + 1 < length:
                       length = end - start + 1
                       window = s[start:end+1]
                   # move left side
                   while all([v <= 0 for v in h.values()]):
                       if s[start] in h:
                           h[s[start]] += 1
                       start += 1
                       if all([v <= 0 for v in h.values()]):
                           if end - start + 1 < length:
                               length = end - start + 1
                               window = s[start:end+1]
           i += 1
           end = i
           
       return window

找到一种记录当前字符有效长度的解法,比我那种all()的解法简直不知道好多少倍

class Solution(object):
    def minWindow(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        
        if len(s) < len(t): 
            return ""
        
        collect = {}
        for item in t:
            if item in collect:
                collect[item] += 1
            else:
                collect[item] = 1
        # 记录最大可能end和start而非记录长度
        maxend = 2147483647        
        maxstart = 0
        start = end = 0
        count = len(t)
        
        while end < len(s):
            if s[end] in collect:
                # 移动右边界,如果右边届正好是collect中缺少的部分,那么就减少count
                if collect[s[end]] > 0: 
                    count -= 1
                collect[s[end]] -= 1 # 注意此时collect中的值可能是负数
            end += 1
            
            while count == 0: # 说明找到一个可能性
                if end - start < maxend - maxstart:
                    maxend, maxstart = end, start
                # 移动左边界
                if s[start] in collect:
                    collect[s[start]] += 1
                    if collect[s[start]] > 0:
                        count += 1
                start += 1
            
        if maxend - maxstart >= 2147483647:
            return ""
                
        return s[maxstart:maxend]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,790评论 0 33
  • LeetCode 刷题随手记 - 第一部分 前 256 题(非会员),仅算法题,的吐槽 https://leetc...
    蕾娜漢默阅读 17,995评论 2 36
  • 一、JS前言 (1)认识JS 也许你已经了解HTML标记(也称为结构),知道了CSS样式(也称为表示),会使用HT...
    凛0_0阅读 2,817评论 0 8
  • 文~范乘风 许多时候 我爱在水边独行 思考和写诗 风吹过两岸的芦苇 鱼眠水底 倦鸟归林 安静得只有影子陪伴 我穿过...
    范乘风阅读 168评论 0 2
  • 我在读这本书前,看了《灵魂有香气的女子》,看了这本书之后我便去国图办了借书证,以前看书基本上都是直接买的,现在有借...
    肥玉书生阅读 745评论 0 1