{"id":2423,"date":"2022-01-26T18:47:23","date_gmt":"2022-01-26T10:47:23","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2423"},"modified":"2022-01-26T18:47:24","modified_gmt":"2022-01-26T10:47:24","slug":"leetcodeday89","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/26\/leetcodeday89\/","title":{"rendered":"leetcodeday89&#8211;\u683c\u96f7\u7f16\u7801"},"content":{"rendered":"\n<p><strong>n \u4f4d\u683c\u96f7\u7801\u5e8f\u5217<\/strong>&nbsp;\u662f\u4e00\u4e2a\u7531&nbsp;<code>2<sup>n<\/sup><\/code>&nbsp;\u4e2a\u6574\u6570\u7ec4\u6210\u7684\u5e8f\u5217\uff0c\u5176\u4e2d\uff1a<\/p>\n\n\n\n<ul><li>\u6bcf\u4e2a\u6574\u6570\u90fd\u5728\u8303\u56f4&nbsp;<code>[0, 2<sup>n<\/sup> - 1]<\/code>&nbsp;\u5185\uff08\u542b&nbsp;<code>0<\/code>&nbsp;\u548c&nbsp;<code>2<sup>n<\/sup> - 1<\/code>\uff09<\/li><li>\u7b2c\u4e00\u4e2a\u6574\u6570\u662f&nbsp;<code>0<\/code><\/li><li>\u4e00\u4e2a\u6574\u6570\u5728\u5e8f\u5217\u4e2d\u51fa\u73b0&nbsp;<strong>\u4e0d\u8d85\u8fc7\u4e00\u6b21<\/strong><\/li><li>\u6bcf\u5bf9&nbsp;<strong>\u76f8\u90bb<\/strong>&nbsp;\u6574\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a&nbsp;<strong>\u6070\u597d\u4e00\u4f4d\u4e0d\u540c<\/strong>&nbsp;\uff0c\u4e14<\/li><li><strong>\u7b2c\u4e00\u4e2a<\/strong>&nbsp;\u548c&nbsp;<strong>\u6700\u540e\u4e00\u4e2a<\/strong>&nbsp;\u6574\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a&nbsp;<strong>\u6070\u597d\u4e00\u4f4d\u4e0d\u540c<\/strong><\/li><\/ul>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570&nbsp;<code>n<\/code>&nbsp;\uff0c\u8fd4\u56de\u4efb\u4e00\u6709\u6548\u7684&nbsp;<strong>n \u4f4d\u683c\u96f7\u7801\u5e8f\u5217<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>n = 2\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;0,1,3,2]\n<strong>\u89e3\u91ca\uff1a<\/strong>\n&#91;0,1,3,2] \u7684\u4e8c\u8fdb\u5236\u8868\u793a\u662f &#91;00,01,11,10] \u3002\n- 0<strong><em>0<\/em><\/strong> \u548c 0<em><strong>1<\/strong><\/em> \u6709\u4e00\u4f4d\u4e0d\u540c\n- <em><strong>0<\/strong><\/em>1 \u548c <em><strong>1<\/strong><\/em>1 \u6709\u4e00\u4f4d\u4e0d\u540c\n- 1<em><strong>1<\/strong><\/em> \u548c 1<em><strong>0<\/strong><\/em> \u6709\u4e00\u4f4d\u4e0d\u540c\n- <em><strong>1<\/strong><\/em>0 \u548c <em><strong>0<\/strong><\/em>0 \u6709\u4e00\u4f4d\u4e0d\u540c\n&#91;0,2,3,1] \u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7684\u683c\u96f7\u7801\u5e8f\u5217\uff0c\u5176\u4e8c\u8fdb\u5236\u8868\u793a\u662f &#91;00,10,11,01] \u3002\n- <em><strong>0<\/strong><\/em>0 \u548c <em><strong>1<\/strong><\/em>0 \u6709\u4e00\u4f4d\u4e0d\u540c\n- 1<em><strong>0<\/strong><\/em> \u548c 1<em><strong>1<\/strong><\/em> \u6709\u4e00\u4f4d\u4e0d\u540c\n- <em><strong>1<\/strong><\/em>1 \u548c <em><strong>0<\/strong><\/em>1 \u6709\u4e00\u4f4d\u4e0d\u540c\n- 0<em><strong>1<\/strong><\/em> \u548c 0<em><strong>0<\/strong><\/em> \u6709\u4e00\u4f4d\u4e0d\u540c<\/code><\/pre>\n\n\n\n<p>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=89 lang=python3\r\n#\r\n# &#91;89] \u683c\u96f7\u7f16\u7801\r\n#\r\n\r\n# @lc code=start\r\nclass Solution:\r\n    def grayCode(self, n: int) -> List&#91;int]:\r\n        def subgrayCode(n):\r\n            mid=list()\r\n            if n==1:\r\n                return &#91;\"0\",\"1\"]\r\n            else:\r\n                res=subgrayCode(n-1)\r\n                lens=len(res)\r\n                for i in range(lens):\r\n                    mid.append(\"0\"+res&#91;i])\r\n                for i in range(lens-1,-1,-1):\r\n                    mid.append(\"1\"+res&#91;i])\r\n                return mid\r\n\r\n        x=subgrayCode(n)\r\n        for i in range(len(x)):\r\n            x&#91;i]=int(x&#91;i],2)\r\n        return x\r\n# @lc code=end<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"798\" height=\"222\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-297.png\" alt=\"\" class=\"wp-image-2424\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-297.png 798w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-297-300x83.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-297-768x214.png 768w\" sizes=\"(max-width: 798px) 100vw, 798px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>n \u4f4d\u683c\u96f7\u7801\u5e8f\u5217&nbsp;\u662f\u4e00\u4e2a\u7531&nbsp;2n&nbsp;\u4e2a\u6574\u6570\u7ec4\u6210\u7684\u5e8f\u5217\uff0c\u5176\u4e2d\uff1a \u6bcf\u4e2a\u6574\u6570\u90fd\u5728\u8303\u56f4&#038;n &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/26\/leetcodeday89\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday89&#8211;\u683c\u96f7\u7f16\u7801<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2423"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2423"}],"version-history":[{"count":1,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2423\/revisions"}],"predecessor-version":[{"id":2425,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2423\/revisions\/2425"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2423"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}