{"id":2154,"date":"2018-03-17T10:31:55","date_gmt":"2018-03-17T17:31:55","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2154"},"modified":"2018-03-17T12:29:03","modified_gmt":"2018-03-17T19:29:03","slug":"leetcode-401-binary-watch","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-401-binary-watch\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 401. Binary Watch"},"content":{"rendered":"<h1>Problem:<\/h1>\n<p>A binary watch has 4 LEDs on the top which represent the\u00a0<b>hours<\/b>\u00a0(<b>0-11<\/b>), and the 6 LEDs on the bottom represent the\u00a0<b>minutes<\/b>\u00a0(<b>0-59<\/b>).<\/p>\n<p>Each LED represents a zero or one, with the least significant bit on the right.<\/p>\n<h1><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/8b\/Binary_clock_samui_moon.jpg\" height=\"300\" \/><\/h1>\n<p>For example, the above binary watch reads &#8220;3:25&#8221;.<\/p>\n<p>Given a non-negative integer\u00a0<i>n<\/i>\u00a0which represents the number of LEDs that are currently on, return all possible times the watch could represent.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon: false\">Input: n = 1\r\nReturn: [\"1:00\", \"2:00\", \"4:00\", \"8:00\", \"0:01\", \"0:02\", \"0:04\", \"0:08\", \"0:16\", \"0:32\"]<\/pre>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li>The order of output does not matter.<\/li>\n<li>The hour must not contain a leading zero, for example &#8220;01:00&#8221; is not valid, it should be &#8220;1:00&#8221;.<\/li>\n<li>The minute must be consist of two digits and may contain a leading zero, for example &#8220;10:2&#8221; is not valid, it should be &#8220;10:02&#8221;.<\/li>\n<\/ul>\n<h1>Solution 1:<\/h1>\n<p>Time complexity: O(11*59*n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 2 ms (beats 100%)\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; readBinaryWatch(int num) {\r\n    vector&lt;string&gt; ans;\r\n    for (int i = 0; i &lt;= num; ++i)\r\n      for (int h : nums(i, 12))\r\n        for (int m : nums(num - i, 60))\r\n          ans.push_back(to_string(h) + (m &lt; 10 ? \":0\" : \":\") + to_string(m));\r\n    return ans;\r\n  }\r\nprivate:\r\n  \/\/ Return numbers in [0,r) that has b 1s in their binary format.\r\n  vector&lt;int&gt; nums(int b, int r) {    \r\n    vector&lt;int&gt; ans;\r\n    for (int n = 0; n &lt; r; ++n)\r\n      if (__builtin_popcount(n) == b) ans.push_back(n);\r\n    return ans;\r\n  }  \r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: A binary watch has 4 LEDs on the top which represent the\u00a0hours\u00a0(0-11), and the 6 LEDs on the bottom represent the\u00a0minutes\u00a0(0-59). Each LED represents&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[16,108,122,222],"class_list":["post-2154","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-clock","tag-combination","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2154","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=2154"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2154\/revisions"}],"predecessor-version":[{"id":2163,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2154\/revisions\/2163"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}