• leetcode-hard-array-76. Minimum Window Substring -NO


    mycode

    不会。。

    参考:

    class Solution(object):
        def minWindow(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: str
            """
            count1 = collections.defaultdict(int)
            count2 = []
            for char in t:
                count1[char] += 1
                count2.append(char)
    
            count = len(t)
            start = 0
            minSize = len(s) + 1
            minStart = 0
    
            for end in range(len(s)):
                if s[end] in count2 :
                    #print(end,count1,count2,count)
                    count1[s[end]] -= 1
                    if count1[s[end]] >= 0:  #<=0时表示出现了多余的t中要求的元素,比如t中两个A,现在出现了第三个A,所以没必要count-1
                        count -= 1
                    if count == 0:
                        while True:                      
                            if s[start] in count2  :
                                if count1[s[start]] < 0:  #s中第一个出现t中元素的位置可以被更换啦
                                    count1[s[start]] += 1
                                else:  #
                                    print(start) 
                                    break
                            start += 1
                        if minSize > end - start + 1:
                            minSize = end - start + 1
                            minStart = start
                    #print(end,count1,count2,count)
                 
            if minSize < len(s) + 1:
                return s[minStart:minStart + minSize]
            else:
                return ''    
            
            class Solution(object):
        def minWindow(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: str
            """
            count1 = collections.defaultdict(int)
            count2 = []
            for char in t:
                count1[char] += 1
                count2.append(char)
    
            count = len(t)
            start = 0
            minSize = len(s) + 1
            minStart = 0
    
            for end in range(len(s)):
                if s[end] in count2 :
                    #print(end,count1,count2,count)
                    count1[s[end]] -= 1
                    if count1[s[end]] >= 0:
                        count -= 1
                    if count == 0:
                        while True:
                          
                            if s[start] in count2  :
                                if count1[s[start]] < 0:
                                    count1[s[start]] += 1
                                else:
                                    print(start)
                                    break
                            start += 1
                        if minSize > end - start + 1:
                            minSize = end - start + 1
                            minStart = start
                    #print(end,count1,count2,count)
                 
            if minSize < len(s) + 1:
                return s[minStart:minStart + minSize]
            else:
                return ''    
            
            
  • 相关阅读:
    贪婪与非贪婪模式
    Arduino语言介绍
    POJ 3249 记忆化搜索或拓扑排序
    POJ 3177 缩点 + 边双连通图
    POJ 1637 网络流构图
    hdu 1285 拓扑排序+优先队列
    POJ 3160 缩点+拓扑排序+简单dp
    POJ 3592 缩点+拓扑排序+最长路
    针对11级队员的暑假训练计划(初稿)
    POJ 2762 缩点+判断是否是最长链
  • 原文地址:https://www.cnblogs.com/rosyYY/p/11044806.html
Copyright © 2020-2023  润新知