{"id":2583,"date":"2018-07-10T22:11:17","date_gmt":"2018-07-10T14:11:17","guid":{"rendered":"https:\/\/nodelay.xyz\/?p=2583"},"modified":"2018-07-10T22:11:17","modified_gmt":"2018-07-10T14:11:17","slug":"leetcode-89-gray-code","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2583","title":{"rendered":"LeetCode 89. Gray Code"},"content":{"rendered":"<p>Divide and conquer!<br \/>\nAdd a leading <span style=\"color: #ff0000;\">0<\/span> or <span style=\"color: #333399;\">1<\/span> bit to your existing results so here comes two sub-results: 0{xxxxx} and 1{xxxxx}. How to merge them? Just keep the first sub group and append the second group items reversely so that there is only a 1-bit difference between the end of group 0 and &#8220;start&#8221; of group 2.<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    vector&lt;int&gt; grayCode(int n) {\n        if (n == 0)\n            return { 0 };\n        vector&lt;int&gt; results = { 0, 1 };\n        for (size_t i = 1; i &lt; n; ++i)\n        {\n            for (int j = results.size() - 1; j &gt;= 0; --j)\n            {\n                results.push_back(results[j] + (1 &lt;&lt; i));\n            }\n        }\n        return results;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Divide and conquer! Add a leading 0 or 1 bit to your existing results so here comes two sub-results: 0{xxxxx} and 1{xxxxx}. How to merge them? Just keep the first sub group and append the second group items reversely so that there is only a 1-bit difference between the end of group 0 and &#8220;start&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[66],"class_list":["post-2583","post","type-post","status-publish","format-standard","hentry","category-technical","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2583"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2583\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}