{"id":3165,"date":"2018-07-14T22:32:45","date_gmt":"2018-07-15T05:32:45","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3165"},"modified":"2018-07-14T22:39:28","modified_gmt":"2018-07-15T05:39:28","slug":"leetcode-869-reordered-power-of-2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-869-reordered-power-of-2\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 869. Reordered Power of 2"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Starting with a positive integer\u00a0<code>N<\/code>, we reorder the digits in any order (including the original order) such that the leading digit is not zero.<\/p>\n<p>Return\u00a0<code>true<\/code>\u00a0if and only if we can do this in a way such that the resulting number is a power of 2.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">1<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">true<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">10<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">false<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">16<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">true<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong><span id=\"example-input-4-1\">24<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-4\">false<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 5:<\/strong><\/p>\n<pre class=\"\"><strong>Input: <\/strong><span id=\"example-input-5-1\">46<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-5\">true<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= N &lt;= 10^9<\/code><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h1><strong>Solution: HashTable<\/strong><\/h1>\n<p>Compare the counter of digit string with that of all power of 2s.<\/p>\n<p>e.g. 64 -&gt; {4: 1, 6: 1} == 46 {4:1, 6: 1}<\/p>\n<p>Time complexity: O(1)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  bool reorderedPowerOf2(int N) {    \r\n    auto m = countMap(N);\r\n    for (int i = 0; i &lt; 31; ++i)\r\n      if (m == countMap(1 &lt;&lt; i)) return true;\r\n    return false;\r\n  }\r\nprivate:\r\n  map&lt;int, int&gt; countMap(int n) {\r\n    map&lt;int, int&gt; m;\r\n    while (n) {\r\n      ++m[n % 10];\r\n      n \/= 10;\r\n    }\r\n    return m;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Starting with a positive integer\u00a0N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[39,16,82,177],"class_list":["post-3165","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-binary","tag-bit","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3165","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=3165"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3165\/revisions"}],"predecessor-version":[{"id":3167,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3165\/revisions\/3167"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}