{"id":197,"date":"2022-01-09T06:49:57","date_gmt":"2022-01-08T22:49:57","guid":{"rendered":"https:\/\/www.wennroy.com\/?p=197"},"modified":"2022-01-09T06:51:03","modified_gmt":"2022-01-08T22:51:03","slug":"89-gray-code","status":"publish","type":"post","link":"https:\/\/wennroy.com\/index.php\/2022\/01\/09\/89-gray-code\/","title":{"rendered":"89. \u683c\u96f7\u7f16\u7801"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">n \u4f4d\u683c\u96f7\u7801\u5e8f\u5217 \u662f\u4e00\u4e2a\u7531 2n \u4e2a\u6574\u6570\u7ec4\u6210\u7684\u5e8f\u5217\uff0c\u5176\u4e2d\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\" id=\"block-c95b67fd-84a6-4b39-b4bb-0880db57f002\"><li>\u6bcf\u4e2a\u6574\u6570\u90fd\u5728\u8303\u56f4 [0, 2n &#8211; 1] \u5185\uff08\u542b 0 \u548c 2n &#8211; 1\uff09<\/li><li>\u7b2c\u4e00\u4e2a\u6574\u6570\u662f 0<\/li><li>\u4e00\u4e2a\u6574\u6570\u5728\u5e8f\u5217\u4e2d\u51fa\u73b0 \u4e0d\u8d85\u8fc7\u4e00\u6b21<\/li><li>\u6bcf\u5bf9 \u76f8\u90bb \u6574\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a \u6070\u597d\u4e00\u4f4d\u4e0d\u540c \uff0c\u4e14<\/li><li>\u7b2c\u4e00\u4e2a \u548c \u6700\u540e\u4e00\u4e2a \u6574\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a \u6070\u597d\u4e00\u4f4d\u4e0d\u540c<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 n \uff0c\u8fd4\u56de\u4efb\u4e00\u6709\u6548\u7684 n \u4f4d\u683c\u96f7\u7801\u5e8f\u5217 \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u6cd5\uff1a\u5bf9\u79f0\u751f\u6210\u3002\u627e\u5230\u89c4\u5f8b\u540e\u751f\u6210\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Solution:\n    def grayCode(self, n: int) -> List[int]:\n        if n == 1:\n            return [0,1]\n        temp_ans = [\"0\",\"1\"]\n        for i in range(n-1):\n            new_temp_ans = []\n            # \u6dfb\u52a00\u5728\u9996\u4f4d\n            for temp in temp_ans:\n                temp = \"0\" + temp\n                new_temp_ans.append(temp)\n            # \u6dfb\u52a01\u5728\u9996\u4f4d\n            for temp in temp_ans[::-1]:\n                temp = \"1\" + temp\n                new_temp_ans.append(temp)\n            temp_ans = new_temp_ans\n        # print(temp_ans)\n        ans = []\n        for temp in temp_ans:\n            ans.append(int(temp,2))\n        return ans<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b98\u65b9\u6807\u7b54\uff1a\u5bf9\u79f0\u751f\u6210<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u77e5\u9053n-1\u7684\u60c5\u51b5\uff0c\u751f\u6210n\u7684\u60c5\u51b5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Solution:\n    def grayCode(self, n: int) -> List[int]:\n        ans = [0]\n        for i in range(1, n + 1):\n            for j in range(len(ans) - 1, -1, -1):\n                ans.append(ans[j] | (1 &lt;&lt; (i - 1)))\n        return ans<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e8c\u8fdb\u5236\u6570\u8f6c\u683c\u96f7\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Solution:\n    def grayCode(self, n: int) -> List[int]:\n        ans = [0] * (1 &lt;&lt; n)\n        for i in range(1 &lt;&lt; n):\n            ans[i] = (i >> 1) ^ i\n        return ans<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09<br>\u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/gray-code<br>\u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u77e5\u8bc6\u8865\u5145<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5173\u4e8ePython\u4f4d\u64cd\u4f5c\u89c4\u5219\uff0c\u4f8b\u5982&#8221;|&#8221;\u3001&#8221;&lt;&lt;&#8220;\u7b49\uff0c\u89c1\u94fe\u63a5<a href=\"https:\/\/blog.csdn.net\/qq_32591415\/article\/details\/111828121\">https:\/\/blog.csdn.net\/qq_32591415\/article\/details\/111828121<\/a><\/li><li>Python \u9006\u5e8f\u6392\u5217\u5207\u7247[::-1].<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>n \u4f4d\u683c\u96f7\u7801\u5e8f\u5217 \u662f\u4e00\u4e2a\u7531 2n \u4e2a\u6574\u6570\u7ec4\u6210\u7684\u5e8f\u5217 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[6,17],"tags":[],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-leetcode","category-binary"],"_links":{"self":[{"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":1,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":200,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions\/200"}],"wp:attachment":[{"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wennroy.com\/index.php\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}