{"id":5625,"date":"2019-09-29T21:17:49","date_gmt":"2019-09-30T04:17:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5625"},"modified":"2019-09-29T21:21:05","modified_gmt":"2019-09-30T04:21:05","slug":"leetcode-89-gray-code","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-89-gray-code\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 89. Gray Code"},"content":{"rendered":"\n<p>The gray code is a binary numeral system where two successive values differ in only one bit.<\/p>\n\n\n\n<p>Given a non-negative integer&nbsp;<em>n<\/em>&nbsp;representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>&nbsp;2\n<strong>Output:<\/strong>&nbsp;<code>[0,1,3,2]<\/code>\n<strong>Explanation:<\/strong>\n00 - 0\n01 - 1\n11 - 3\n10 - 2\n\nFor a given&nbsp;<em>n<\/em>, a gray code sequence may not be uniquely defined.\nFor example, [0,2,3,1] is also a valid gray code sequence.\n\n00 - 0\n10 - 2\n11 - 3\n01 - 1\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>&nbsp;0\n<strong>Output:<\/strong>&nbsp;<code>[0]\n<strong>Explanation:<\/strong> We define the gray code sequence to begin with 0.\n&nbsp;            A gray code sequence of <em>n<\/em> has size = 2<sup>n<\/sup>, which for <em>n<\/em> = 0 the size is 2<sup>0<\/sup> = 1.\n&nbsp;            Therefore, for <em>n<\/em> = 0 the gray code sequence is [0].<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[0] = 0<br>dp[i] = dp[i &#8211; 1] + [x | 1 &lt;&lt; (i &#8211; 1) for x in reversed(dp[i &#8211; 1])]<\/p>\n\n\n\n<p>Time complexity: O(2^n)<br>Space complexity: O(2^n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> grayCode(int n) {\n    vector<vector<int>> dp(n + 1);\n    dp[0] = {0};    \n    for (int i = 1; i <= n; ++i) {\n      dp[i] = dp[i - 1];\n      for (int j = dp[i - 1].size() - 1; j >= 0; --j)\n        dp[i].push_back(dp[i - 1][j] | (1 << (i - 1)));\n    }\n    return dp[n];\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def grayCode(self, n: int) -> List[int]:    \n    dp = [0]\n    for i in range(n):      \n      dp = dp + [x | 1 << i for x in reversed(dp)]\n    return dp\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer&nbsp;n&nbsp;representing the total number of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,177],"class_list":["post-5625","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5625"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5625\/revisions"}],"predecessor-version":[{"id":5629,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5625\/revisions\/5629"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}