{"id":2529,"date":"2018-04-12T11:30:42","date_gmt":"2018-04-12T03:30:42","guid":{"rendered":"https:\/\/boweihe.me\/?p=2529"},"modified":"2018-04-12T11:30:42","modified_gmt":"2018-04-12T03:30:42","slug":"leetcode-76-minimum-window-substring","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2529","title":{"rendered":"LeetCode 76. Minimum Window Substring"},"content":{"rendered":"<p><a href=\"https:\/\/leetcode.com\/problems\/minimum-window-substring\/description\/\" target=\"_blank\" rel=\"noopener noreferrer\">76.\u00a0Minimum Window Substring<\/a><br \/>\n\u601d\u8def:<br \/>\n\u6784\u9020\u4e00\u4e2a\u54c8\u5e0c\u8868\u7ed3\u6784\u7edf\u8ba1\u6211\u4eec\u7684\u9700\u6c42\uff0c\u4e5f\u5c31\u662f\u5b57\u7b26\u4e32t\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u6570\u91cf\uff0c\u65b9\u4fbf\u8d77\u89c1\u5982\u679c\u6709\u9700\u6c42\u5219\u54c8\u5e0c\u8868\u7684\u5bf9\u5e94\u503c-1\u3002\u53e6\u5916\u5b58\u50a8\u4e00\u4e0b\u5f53\u524d\u89e3\u7684\u8d77\u59cb\u4f4d\u7f6eSTART\u4ee5\u53ca\u6700\u4f18\u89e3\u7684\u4f4d\u7f6e\u3002<br \/>\n\u626b\u63cf\u5b57\u7b26\u4e32s\uff0c\u5728\u4f4d\u7f6es[i]\u65f6\u5982\u679c\u80fd\u591f\u6ee1\u8db3\u9700\u6c42\uff08\u5373\u54c8\u5e0c\u8868\u4e2d\u6ca1\u6709\u8d1f\u503c\uff09\uff0c\u5219\u4eceSTART\u5f00\u59cb\u6e05\u9664\u65e0\u7528\u7684\u5b57\u7b26\u76f4\u81f3\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u4f4d\u7f6e\u3002\u6bd4\u5982\u9700\u6c42\u662fA:1, B:2\uff0cSTART\u5f00\u59cb\u7684\u5b57\u7b26\u662f<span style=\"color: #3366ff;\">DEF<\/span>BAB\uff0c\u5219\u6e05\u9664DEF\u3002\u6e05\u9664\u5b8c\u6210\u540e\u66f4\u65b0START\u503c\u5e76\u8ddf\u5f53\u524d\u5df2\u77e5\u7684\u6700\u4f18\u89e3\u6bd4\u8f83\uff0c\u66f4\u65b0\u6700\u4f18\u89e3\u3002<br \/>\n\u5570\u55e6\u4e00\u53e5\uff0c\u4f3c\u4e4e\u76f8\u5f53\u4e00\u90e8\u5206substring\u7c7b\u578b\u7684\u9898\u76ee\u90fd\u80fd\u7528hash table + two pointers\u7684\u65b9\u5f0f\u89e3\u51b3\u3002<\/p>\n<pre class=\"theme:tomorrow-night lang:c++ decode:true\">class Solution {\npublic:\n    static string minWindow(string s, string t) {\n        int count_map[128] = {0};\n        set&lt;char&gt; req_set;\n        int start_index = -1, min_start_index = 0;\n        int min_end_index = INT_MAX;\n        for (const char&amp; c : t)\n        {\n            count_map[c] --;\n            req_set.insert(c);\n        }\n        for (int i = 0; i &lt; s.size(); ++i)\n        {\n            const char c = s[i];\n            count_map[c] ++;\n            bool require_sufficed = true;\n            for (const char&amp; c : req_set)\n            {\n                if (count_map[c] &lt; 0)\n                {\n                    require_sufficed = false;\n                    break;\n                }\n            }\n            if (!require_sufficed) continue;\n            \/\/ clear left\n            for (int j = start_index + 1; j &lt;= i; ++j)\n            {\n                if (--count_map[s[j]] &lt; 0)\n                {\n                    start_index = j;\n                    break;\n                }\n                start_index = j;\n            }\n            if (i - start_index &lt; min_end_index - min_start_index)\n            {\n                min_start_index = start_index;\n                min_end_index = i;\n            }\n        }\n        if (min_end_index == INT_MAX)\n            return \"\";\n        else\n            return s.substr(min_start_index, min_end_index - min_start_index + 1);\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>76.\u00a0Minimum Window Substring \u601d\u8def: \u6784\u9020\u4e00\u4e2a\u54c8\u5e0c\u8868\u7ed3\u6784\u7edf\u8ba1\u6211\u4eec\u7684\u9700\u6c42\uff0c\u4e5f\u5c31\u662f\u5b57\u7b26\u4e32t\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u6570\u91cf\uff0c\u65b9\u4fbf\u8d77\u89c1\u5982\u679c\u6709\u9700\u6c42\u5219\u54c8\u5e0c\u8868\u7684\u5bf9\u5e94\u503c-1\u3002\u53e6\u5916\u5b58\u50a8\u4e00\u4e0b\u5f53\u524d\u89e3\u7684\u8d77\u59cb\u4f4d\u7f6eSTART\u4ee5\u53ca\u6700\u4f18\u89e3\u7684\u4f4d\u7f6e\u3002 \u626b\u63cf\u5b57\u7b26\u4e32s\uff0c\u5728\u4f4d\u7f6es[i]\u65f6\u5982\u679c\u80fd\u591f\u6ee1\u8db3\u9700\u6c42\uff08\u5373\u54c8\u5e0c\u8868\u4e2d\u6ca1\u6709\u8d1f\u503c\uff09\uff0c\u5219\u4eceSTART\u5f00\u59cb\u6e05\u9664\u65e0\u7528\u7684\u5b57\u7b26\u76f4\u81f3\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u4f4d\u7f6e\u3002\u6bd4\u5982\u9700\u6c42\u662fA:1, B:2\uff0cSTART\u5f00\u59cb\u7684\u5b57\u7b26\u662fDEFBAB\uff0c\u5219\u6e05\u9664DEF\u3002\u6e05\u9664\u5b8c\u6210\u540e\u66f4\u65b0START\u503c\u5e76\u8ddf\u5f53\u524d\u5df2\u77e5\u7684\u6700\u4f18\u89e3\u6bd4\u8f83\uff0c\u66f4\u65b0\u6700\u4f18\u89e3\u3002 \u5570\u55e6\u4e00\u53e5\uff0c\u4f3c\u4e4e\u76f8\u5f53\u4e00\u90e8\u5206substring\u7c7b\u578b\u7684\u9898\u76ee\u90fd\u80fd\u7528hash table + two pointers\u7684\u65b9\u5f0f\u89e3\u51b3\u3002 class Solution { public: static string minWindow(string s, string t) { int count_map[128] = {0}; set&lt;char&gt; req_set; int start_index = -1, min_start_index = 0; int min_end_index = INT_MAX; for (const char&amp; c : t) { count_map[c] &#8211;; req_set.insert(c); } for (int i = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-2529","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2529"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2529\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}