{"id":3107,"date":"2020-03-01T18:44:31","date_gmt":"2020-03-01T10:44:31","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=3107"},"modified":"2020-03-01T18:44:31","modified_gmt":"2020-03-01T10:44:31","slug":"%e3%80%90leetcode%e3%80%910076-%e6%9c%80%e5%b0%8f%e8%a6%86%e7%9b%96%e5%ad%90%e4%b8%b2","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/3107\/","title":{"rendered":"\u3010LeetCode\u30110076 \u6700\u5c0f\u8986\u76d6\u5b50\u4e32"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/minimum-window-substring\/\">https:\/\/leetcode-cn.com\/problems\/minimum-window-substring\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def minWindow(self, s, t):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> s: str\n        <\/em><strong><em>:type<\/em><\/strong><em> t: str\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: str\n        \"\"\"\n        <\/em>weight_list = []\n        str_list = []\n        temp_weight = 0\n        for i in range(len(s)):\n            if s[i] not in t:\n                temp_weight += 1\n                continue\n            temp_weight += 1\n            str_list.append(s[i])\n            weight_list.append(temp_weight)\n            temp_weight = 0\n        windows_length = len(t)\n        current_min_idx = -1\n        current_min_val = float('inf')\n        for i in range(len(str_list) - windows_length + 1):\n            queue = []\n            temp_val = 0\n            for j in range(windows_length):\n                if len(queue) != len(t) and str_list[i + j] not in queue:\n                    queue.append(str_list[i + j])\n                    if j != 0:\n                        temp_val += weight_list[i + j]\n                    else:\n                        temp_val += 1\n                if len(queue) == len(t):\n                    if temp_val &lt; current_min_val:\n                        current_min_idx = i\n                        current_min_val = temp_val\n        if current_min_idx == -1:\n            return ''\n        from_idx = 0\n        for i in range(current_min_idx + 1):\n            from_idx += weight_list[i]\n        return s[from_idx - 1:from_idx + current_min_val - 1]\nif __name__ == '__main__':\n    S = \"aa\"\n    T = \"aa\"\n    # S = \"ADOBECODEBANC\"\n    # T = \"ABC\"\n    print(Solution().minWindow(S, T))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u6211\u611f\u89c9\u6211\u601d\u8def\u633a\u597d\u7684\uff0c\u590d\u6742\u5ea6\u5e94\u8be5\u3002\u3002\uff1f\u6211\u4e5f\u4e0d\u77e5\u9053\u591a\u5c11\uff0c\u6700\u574fn^2\u5e94\u8be5\u662f\u3002<\/p>\n\n\n<p>\u9996\u5148\u5c31\u662f\u628a\u65e0\u5173\u5b57\u7b26\u5168\u53bb\u6389\uff0c\u6bd4\u5982ADOBECODEBANC\u76f4\u63a5\u8fc7\u6ee4\u6210ABCBAC\uff0c\u7136\u540e\u6bcf\u4e00\u4e2a\u5b57\u6bcd\u90fd\u5e26\u4e00\u4e2a\u6743\u91cd\uff0c\u4ee3\u8868\u5f53\u524d\u5b57\u6bcd\u540en\u4e2a\u957f\u5ea6\u4e3a\u4e0b\u4e00\u4e2at\u4e2d\u7684\u5b57\u7b26\uff0c\u6bd4\u5982ABCBAC\u5bf9\u5e94\u4e00\u4e2a\u6743\u91cd\u5c31\u662f1\uff0c3\uff0c2\uff0c4\uff0c1\uff0c2\u3002\u56e0\u6b64\u95ee\u9898\u5c31\u53d8\u6210\u4e86\u627e\u6700\u77ed\u6743\u91cd\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n<p>\u6211\u4eec\u5229\u7528\u4e00\u4e2at\u957f\u5ea6\u7684\u7a97\u53e3\uff0c\u5224\u65ad\u7a97\u53e3\u5185\u662f\u4e0d\u662ft\u4e2d\u7684\u5b57\u7b26\uff0c\u7136\u540e\u7b97\u4e00\u4e0b\u6743\u91cd\uff0c\u627e\u5230\u6743\u91cd\u6700\u4f4e\u7684\u7a97\u53e3\uff0c\u6839\u636e\u6743\u91cd\u5c31\u53ef\u4ee5\u6620\u5c04\u5230\u539f\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u8303\u56f4\u3002<\/p>\n\n\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u662f\u5217\u8868\u8fdb\u884c\u91cd\u590d\u5224\u65ad\uff0c\u4f46\u662f\u5b9e\u9645\u4e0at\u662f\u5141\u8bb8\u91cd\u590d\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u628a\u5217\u8868\u53d8\u6210\u5b57\u5178\uff0c\u591a\u5faa\u73af\u4e00\u904dt\u5c31\u53ef\u4ee5\u4e86\u3002\u592a\u61d2\u4e86\u5c31\u4e0d\u5199\u4e86\uff0c\u4f46\u662f\u601d\u8def\u80af\u5b9a\u662f\u5bf9\u7684\uff0c\u5bf9\u4e0d\u8d77\u5bf9\u4e0d\u8d77\u5bf9\u4e0d\u8d77\u5bf9\u4e0d\u8d77\uff0c\u4eca\u5929\u51fa\u95e8\u4e00\u5929\uff0c\u665a\u4e0a\u624d\u56de\u6765\uff0c\u5b9e\u5728\u662f\u4e0d\u60f3\u6539\u8fd9\u70b9\u4ee3\u7801\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/minimum-window-su [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":2703,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3107"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3107"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3107\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3107"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}