{"id":623,"date":"2020-09-26T11:04:34","date_gmt":"2020-09-26T03:04:34","guid":{"rendered":"http:\/\/39.96.58.60\/?p=623"},"modified":"2022-10-18T16:45:04","modified_gmt":"2022-10-18T08:45:04","slug":"leetcode-316-%e5%8e%bb%e9%99%a4%e9%87%8d%e5%a4%8d%e5%ad%97%e6%af%8d","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=623","title":{"rendered":"Leetcode 316. \u53bb\u9664\u91cd\u590d\u5b57\u6bcd"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>s<\/code> \uff0c\u8bf7\u4f60\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u91cd\u590d\u7684\u5b57\u6bcd\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u5b57\u6bcd\u53ea\u51fa\u73b0\u4e00\u6b21\u3002\u9700\u4fdd\u8bc1 <strong>\u8fd4\u56de\u7ed3\u679c\u7684\u5b57\u5178\u5e8f\u6700\u5c0f<\/strong>\uff08\u8981\u6c42\u4e0d\u80fd\u6253\u4e71\u5176\u4ed6\u5b57\u7b26\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff09\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u8be5\u9898\u4e0e 1081 <a href=\"https:\/\/leetcode-cn.com\/problems\/smallest-subsequence-of-distinct-characters\">https:\/\/leetcode-cn.com\/problems\/smallest-subsequence-of-distinct-characters<\/a> \u76f8\u540c<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong><code>s = \"bcabc\"<\/code>\n\n<strong>\u8f93\u51fa<code>\uff1a<\/code><\/strong><code>\"abc\"<\/code>\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong><code>s = \"cbacdcbc\"<\/code>\n\n<strong>\u8f93\u51fa\uff1a<\/strong><code>\"acdb\"<\/code><\/pre>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= s.length &lt;= 10<sup>4<\/sup><\/code><\/li>\n<li><code>s<\/code> \u7531\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7ec4\u6210<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6808\u3001 \u8d2a\u5fc3\u7b97\u6cd5\u3001 \u5b57\u7b26\u4e32\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8698.45% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.09% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def removeDuplicateLetters(self, s: str) -&gt; str:\n        record = {} # \u8bb0\u5f55\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\n        for c in s:\n            if c in record:\n                record[c] += 1\n            else:\n                record[c] = 1\n        result = []     # \u4fdd\u5b58\u6700\u7ec8\u7ed3\u679c\n\n        def insert(c):  # \u6267\u884c\u63d2\u5165c\u5b57\u7b26\n            if c not in result: # \u5f53\u524d\u7ed3\u679c\u4e2d\u4e0d\u5b58\u5728c\u5b57\u7b26\n                if result and result[-1] &gt;= c and record[result[-1]] &gt; 1:   # \u5982\u8fc7\u7ed3\u679c\u4e2d\u524d\u4e00\u4e2a\u5b57\u7b26\u5b58\u5728\u591a\u4e2a\uff0c\u4e14\u6bd4\u5f53\u524d\u5b57\u7b26c\u5927\uff0c \u5219\u5c06\u524d\u4e00\u4e2a\u5b57\u7b26\u51fa\u6808\n                    record[result.pop()] -= 1       # \u524d\u4e00\u4e2a\u5b57\u7b26\u4ece\u7edf\u8ba1\u6570-1\n                    insert(c)   # \u63a5\u7740\u5c1d\u8bd5\u63d2\u5165c\u5b57\u7b26\n                elif c not in result:\n                    result.append(c)\n                    # record[c] -= 1\n            else:   # \u5982\u679c\u5f53\u524d\u7ed3\u679c\u4e2d\u5b58\u5728\u5b57\u7b26c\uff0c\u8df3\u8fc7\uff0c\u5b57\u5178\u4e2dc\u7684\u7edf\u8ba1-1\n                record[c] -= 1\n        for c in s: # \u6328\u4e2a\u63d2\u5165s\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\n            insert(c)\n        return ''.join(result)\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s \uff0c\u8bf7\u4f60\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u91cd\u590d\u7684\u5b57\u6bcd\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u5b57\u6bcd\u53ea\u51fa\u73b0\u4e00\u6b21\u3002\u9700\u4fdd\u8bc1 \u8fd4\u56de\u7ed3\u679c\u7684\u5b57\u5178\u5e8f\u6700\u5c0f\uff08\u8981\u6c42\u4e0d&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/623"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=623"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/623\/revisions"}],"predecessor-version":[{"id":624,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/623\/revisions\/624"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}