{"id":750,"date":"2020-09-26T11:18:20","date_gmt":"2020-09-26T03:18:20","guid":{"rendered":"http:\/\/39.96.58.60\/?p=750"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-%e5%89%91%e6%8c%87-offer-38-%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%8e%92%e5%88%97","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=750","title":{"rendered":"Leetcode \u5251\u6307 Offer 38. \u5b57\u7b26\u4e32\u7684\u6392\u5217"},"content":{"rendered":"<p>\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6253\u5370\u51fa\u8be5\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u6240\u6709\u6392\u5217\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f60\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u4f46\u91cc\u9762\u4e0d\u80fd\u6709\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"abc\"\n\n<strong>\u8f93\u51fa\uff1a[<\/strong>\"abc\",\"acb\",\"bac\",\"bca\",\"cab\",\"cba\"<strong>]<\/strong>\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u9650\u5236\uff1a<\/strong><\/p>\n<p><code>1 &lt;= s \u7684\u957f\u5ea6 &lt;= 8<\/code><\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u56de\u6eaf\u7b97\u6cd5\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\uff1a220 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8634.50% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a18.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8649.56% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\u3002\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5217\u8868\uff0c\u6392\u5e8f\u540e\uff0c\u8df3\u8fc7\u91cd\u590d\u5143\u7d20\n\"\"\"\nclass Solution:\n    def permutation(self, s: str) -&gt; List[str]:\n        n = len(s)\n        s = list(s)\n        s.sort()\n        result = []\n        def backtrack(current, used):\n            if len(current) &gt;= n:\n                result.append(''.join(current[:]))\n                return\n            for i in range(n):\n                if i not in used:\n                    if i&gt;0 and s[i] == s[i-1] and i-1 not in used:\n                        continue\n                    backtrack(current+[s[i]], used+[i])\n\n        backtrack([], [])\n        return result\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a176 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8654.66% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a18.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8681.43% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n\"\"\"\nclass Solution:\n    def permutation(self, s: str) -&gt; List[str]:\n        n = len(s)\n        result = []\n\n        def backtrack(s, current):  # \u5269\u4f59\u5b57\u7b26\u4e32\uff0c \u5f53\u524d\u5143\u7d20\n            if len(current) &gt;= n:\n                result.append(''.join(current[:]))\n                return\n            for i in range(len(s)):\n                if s[i] in s[:i]:   #\u8df3\u8fc7\u5f53\u524d\u5b57\u7b26\u4e32\u7684\u91cd\u590d\u5143\u7d20\n                    continue\n                backtrack(s[:i]+s[i+1:], current+[s[i]])\n        backtrack(s, [])\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6253\u5370\u51fa\u8be5\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u6240\u6709\u6392\u5217\u3002 &nbsp; \u4f60\u53ef\u4ee5\u4ee5\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u4f46\u91cc\u9762\u4e0d\u80fd&#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":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/750"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=750"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/750\/revisions"}],"predecessor-version":[{"id":751,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/750\/revisions\/751"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}