{"id":705,"date":"2020-09-26T11:12:43","date_gmt":"2020-09-26T03:12:43","guid":{"rendered":"http:\/\/39.96.58.60\/?p=705"},"modified":"2022-10-18T16:38:16","modified_gmt":"2022-10-18T08:38:16","slug":"leetcode-1415-%e9%95%bf%e5%ba%a6%e4%b8%ba-n-%e7%9a%84%e5%bc%80%e5%bf%83%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e5%ad%97%e5%85%b8%e5%ba%8f%e7%ac%ac-k-%e5%b0%8f%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=705","title":{"rendered":"Leetcode 1415. \u957f\u5ea6\u4e3a n \u7684\u5f00\u5fc3\u5b57\u7b26\u4e32\u4e2d\u5b57\u5178\u5e8f\u7b2c k \u5c0f\u7684\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>\u4e00\u4e2a \u300c\u5f00\u5fc3\u5b57\u7b26\u4e32\u300d\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<ul>\n<li>\u4ec5\u5305\u542b\u5c0f\u5199\u5b57\u6bcd&nbsp;<code>['a', 'b', 'c']<\/code>.<\/li>\n<li>\u5bf9\u6240\u6709\u5728&nbsp;<code>1<\/code>&nbsp;\u5230&nbsp;<code>s.length - 1<\/code>&nbsp;\u4e4b\u95f4\u7684&nbsp;<code>i<\/code>&nbsp;\uff0c\u6ee1\u8db3&nbsp;<code>s[i] != s[i + 1]<\/code>&nbsp;\uff08\u5b57\u7b26\u4e32\u7684\u4e0b\u6807\u4ece 1 \u5f00\u59cb\uff09\u3002<\/li>\n<\/ul>\n<p>\u6bd4\u65b9\u8bf4\uff0c\u5b57\u7b26\u4e32&nbsp;<strong>&#8220;abc&#8221;<\/strong>\uff0c<strong>&#8220;ac&#8221;\uff0c&#8221;b&#8221;<\/strong> \u548c&nbsp;<strong>&#8220;abcbabcbcb&#8221;<\/strong>&nbsp;\u90fd\u662f\u5f00\u5fc3\u5b57\u7b26\u4e32\uff0c\u4f46\u662f&nbsp;<strong>&#8220;aa&#8221;<\/strong>\uff0c<strong>&#8220;baa&#8221;<\/strong>&nbsp;\u548c&nbsp;<strong>&#8220;ababbc&#8221;<\/strong>&nbsp;\u90fd\u4e0d\u662f\u5f00\u5fc3\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u7ed9\u4f60\u4e24\u4e2a\u6574\u6570 <code>n<\/code>&nbsp;\u548c <code>k<\/code>&nbsp;\uff0c\u4f60\u9700\u8981\u5c06\u957f\u5ea6\u4e3a <code>n<\/code>&nbsp;\u7684\u6240\u6709\u5f00\u5fc3\u5b57\u7b26\u4e32\u6309\u5b57\u5178\u5e8f\u6392\u5e8f\u3002<\/p>\n<p>\u8bf7\u4f60\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u7b2c k \u4e2a\u5f00\u5fc3\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u957f\u5ea6\u4e3a <code>n<\/code>&nbsp;\u7684\u5f00\u5fc3\u5b57\u7b26\u4e32\u5c11\u4e8e <code>k<\/code>&nbsp;\u4e2a\uff0c\u90a3\u4e48\u8bf7\u4f60\u8fd4\u56de <strong>\u7a7a\u5b57\u7b26\u4e32<\/strong>&nbsp;\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>n = 1, k = 3\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"c\"\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5217\u8868 [\"a\", \"b\", \"c\"] \u5305\u542b\u4e86\u6240\u6709\u957f\u5ea6\u4e3a 1 \u7684\u5f00\u5fc3\u5b57\u7b26\u4e32\u3002\u6309\u7167\u5b57\u5178\u5e8f\u6392\u5e8f\u540e\u7b2c\u4e09\u4e2a\u5b57\u7b26\u4e32\u4e3a \"c\" \u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>n = 1, k = 4\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"\"\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u957f\u5ea6\u4e3a 1 \u7684\u5f00\u5fc3\u5b57\u7b26\u4e32\u53ea\u6709 3 \u4e2a\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>n = 3, k = 9\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"cab\"\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u957f\u5ea6\u4e3a 3 \u7684\u5f00\u5fc3\u5b57\u7b26\u4e32\u603b\u5171\u6709 12 \u4e2a [\"aba\", \"abc\", \"aca\", \"acb\", \"bab\", \"bac\", \"bca\", \"bcb\", \"cab\", \"cac\", \"cba\", \"cbc\"] \u3002\u7b2c 9 \u4e2a\u5b57\u7b26\u4e32\u4e3a \"cab\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>n = 2, k = 7\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 5\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>n = 10, k = 100\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"abacbabacb\"\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= n &lt;= 10<\/code><\/li>\n<li><code>1 &lt;= k &lt;= 100<\/code><\/li>\n<\/ul>\n<p>&nbsp;<\/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\uff1a224 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8619.92% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a17 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8627.04% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u627e\u51fa\u6240\u6709\u7684\u7ed3\u679c\n\"\"\"\nclass Solution:\n    def getHappyString(self, n: int, k: int) -&gt; str:\n        letter = ['a', 'b', 'c']\n        result = []\n\n        def backtrack(current):\n            if len(current) &gt;= n:   # \u957f\u5ea6\u9650\u5b9a\uff0c\u5982\u679c\u957f\u5ea6\u8fbe\u5230n,\u5219\u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\uff0c\u5e76\u7ec8\u6b62\n                result.append(''.join(current))\n                return\n\n            for i in range(3):\n                if current == [] or (letter[i] != current[-1]): # \u5f53\u524d\u5217\u8868\u4e3a[] \u6216 \u5f53\u524d\u5b57\u6bcd\u4e0e\u5f53\u524d\u5217\u8868\u6700\u540e\u4e00\u4e2a\u4e0d\u540c\u65f6\uff0c \u6dfb\u52a0\u5230\u5f53\u524d\u5217\u8868\n                    current.append(letter[i])\n                    backtrack(current)\n                    current.pop()   # \u56de\u6eaf\n\n        backtrack([])\n        if k &gt; len(result):\n            return ''\n        else:\n            return result[k-1]\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8694.74% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8693.71% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5206\u6790\u72b6\u6001\uff0c\u627e\u89c4\u5f8b\n    \n    1   a       b       c\n    2   ab      ac      ba      bc      ca      cb\n    3   aba     abc     aca     acb     bab     bac     bca     bcb     cab     cac     cba     cbc\n    4   abab    abac    abca    abcb    acab    acac    acba    acbc    baba    babc    baca    bacb    ...\n\n                  all               a\u5f00\u5934     b\u5f00\u5934     c\u5f00\u5934\n    1       3*2**(1-1) = 3*1        1           1       1    \n    2       3*2**(2-1) = 3*2        2           2       2\n    3       3*2**(3-1) = 3*4        4           4       4\n    4       3*2**(4-1) = 3*8        8           8       8\n    10      3*2**(10-1)= 3*512      512         512     512    \n    \u9700\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u4ee5b\u5f00\u5934\u65f6\uff0c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662fa\uff0c\u7b2c\u4e8c\u4e2a\u662fc,\u9700\u5c06\u4e0a\u4e00\u4f4d\u5b58\u5728\u7684\u5143\u7d20\u5254\u9664\n\"\"\"\n\n\nclass Solution:\n    def getHappyString(self, n: int, k: int) -&gt; str:\n\n        all_num = 3 * 2 ** (n - 1)\n        if k &gt; all_num:\n            return ''\n        result = []\n\n        def find(i, k, pre):    # \u5f53\u524d\u4e3a\u4f4d\u6570\uff0c\u5f53\u524d\u627e\u7b2c\u51e0\u4e2a\u6570\uff0c\u524d\u4e00\u4e2a\u5b57\u7b26\n            letters = ['a', 'b', 'c']\n            if pre in letters:\n                letters.remove(pre) # \u4ecea,b,c \u4e2d\u79fb\u9664\uff0c\u4e0a\u4e00\u4e2a\u5b57\u7b26\u3002\u786e\u4fdd\u76f8\u90bb\u5143\u7d20\u4e0d\u540c\n            index = k \/\/ 2 ** (n - 1 - i)   # \u6574\u9664\u83b7\u5f97\u4e0b\u6807\uff0c\u4f9d\u636e\u4e0b\u6807\u53d6\u5f53\u524d\u4f4d\u7684\u5b57\u7b26\n            k = k % 2 ** (n - 1 - i)        # \u4f59\u6570\uff0c\u4f5c\u4e3a\u65b0\u7684k\n            result.append(letters[index])\n            return k, letters[index]\n\n        pre = ''\n        k = k - 1\n        for i in range(n):\n            k, pre = find(i, k, pre)\n        return ''.join(result)\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a \u300c\u5f00\u5fc3\u5b57\u7b26\u4e32\u300d\u5b9a\u4e49\u4e3a\uff1a \u4ec5\u5305\u542b\u5c0f\u5199\u5b57\u6bcd&nbsp;[&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]. \u5bf9\u6240\u6709\u5728&nbsp;&#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\/705"}],"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=705"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":706,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/705\/revisions\/706"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}