{"id":343,"date":"2020-09-26T10:28:56","date_gmt":"2020-09-26T02:28:56","guid":{"rendered":"http:\/\/39.96.58.60\/?p=343"},"modified":"2022-10-18T16:39:50","modified_gmt":"2022-10-18T08:39:50","slug":"leetcode-401-%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%89%8b%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=343","title":{"rendered":"Leetcode 401. \u4e8c\u8fdb\u5236\u624b\u8868"},"content":{"rendered":"<p>\u4e8c\u8fdb\u5236\u624b\u8868\u9876\u90e8\u6709 4 \u4e2a LED \u4ee3\u8868<strong> \u5c0f\u65f6\uff080-11\uff09<\/strong>\uff0c\u5e95\u90e8\u7684 6 \u4e2a LED \u4ee3\u8868<strong> \u5206\u949f\uff080-59\uff09<\/strong>\u3002<\/p>\n<p>\u6bcf\u4e2a LED \u4ee3\u8868\u4e00\u4e2a 0 \u6216 1\uff0c\u6700\u4f4e\u4f4d\u5728\u53f3\u4fa7\u3002<\/p>\n<p><img src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/8b\/Binary_clock_samui_moon.jpg\" style=\"height: 300px;\"><\/p>\n<p>\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u4e8c\u8fdb\u5236\u624b\u8868\u8bfb\u53d6 \u201c3:25\u201d\u3002<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570 <em>n&nbsp;<\/em>\u4ee3\u8868\u5f53\u524d LED \u4eae\u7740\u7684\u6570\u91cf\uff0c\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u65f6\u95f4\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\u8f93\u5165: n = 1\n\n\u8fd4\u56de: [\"1:00\", \"2:00\", \"4:00\", \"8:00\", \"0:01\", \"0:02\", \"0:04\", \"0:08\", \"0:16\", \"0:32\"]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u8f93\u51fa\u7684\u987a\u5e8f\u6ca1\u6709\u8981\u6c42\u3002<\/li>\n<li>\u5c0f\u65f6\u4e0d\u4f1a\u4ee5\u96f6\u5f00\u5934\uff0c\u6bd4\u5982 \u201c01:00\u201d&nbsp;\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u5e94\u4e3a \u201c1:00\u201d\u3002<\/li>\n<li>\u5206\u949f\u5fc5\u987b\u7531\u4e24\u4f4d\u6570\u7ec4\u6210\uff0c\u53ef\u80fd\u4f1a\u4ee5\u96f6\u5f00\u5934\uff0c\u6bd4\u5982 \u201c10:2\u201d&nbsp;\u662f\u65e0\u6548\u7684\uff0c\u5e94\u4e3a \u201c10:02\u201d\u3002<\/li>\n<li>\u8d85\u8fc7\u8868\u793a\u8303\u56f4\uff08<strong>\u5c0f\u65f6 0-11\uff0c\u5206\u949f 0-59<\/strong>\uff09\u7684\u6570\u636e\u5c06\u4f1a\u88ab\u820d\u5f03\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u4f1a\u51fa\u73b0 &#8220;13:00&#8221;, &#8220;0:61&#8221; \u7b49\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u4f4d\u8fd0\u7b97\u3001 \u56de\u6eaf\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a48 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8648.88% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8694.96% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf, \u53c2\u8003https:\/\/leetcode-cn.com\/problems\/binary-watch\/solution\/hui-su-jian-zhi-qu-zhong-by-boille\/\n    \u5c06\u4ee3\u8868\u65f6\u5206\u7684\u706f\u6392\u5217\u5230\u4e00\u8d77\n        0   1   2   3   4   5   6   7   8   9\n        0   2   4   8   0   2   4   8   16  32\n        h   h   h   h   m   m   m   m   m   m\n\n    \u524d\u56db\u4e2a\u4e3a\u4ee3\u8868\u5c0f\u65f6\u7684\u706f\uff0c \u540e\u516d\u4e2a\u4e3a\u4ee3\u8868\u5206\u949f\u7684\u706f\n\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def readBinaryWatch(self, num: int) -&gt; List[str]:\n        result = []         # \u6700\u7ec8\u7ed3\u679c\n        hour_seen = set()   # \u7528\u4e8e\u8bb0\u5f55\u7528\u8fc7\u7684\u5c0f\u65f6\u7684\u706f \u7684\u5e8f\u53f7\n        minute_seen = set() # \u7528\u4e8e\u8bb0\u5f55\u7528\u8fc7\u7684\u5206\u949f\u7684\u706f \u7684\u5e8f\u53f7\n\n        def backstrace(num:int, hour:int, minute:int, which:int) -&gt; None:\n            \"\"\"\n\n            :param num:     \u706f\u7684\u6570\u91cf\n            :param hour:    \u5f53\u524d\u5c0f\u65f6\n            :param minute:  \u5f53\u524d\u5206\u949f\n            :param which:   \u5f53\u524d\u5f00\u59cb\u904d\u5386\u7684\u706f\u7684\u5e8f\u53f7\uff0c \u53bb\u91cd\u4f7f\u7528\u3002\n            :return:\n            \"\"\"\n            if hour &gt; 11 or minute &gt; 59:    # \u7ec8\u6b62\u6761\u4ef6\u3002\u5c0f\u65f6\u5927\u4e8e11\uff0c \u5206\u949f\u5927\u4e8e59\n                return\n\n            if num == 0:    # \u7b49\u4e8e0\u65f6\uff0c\u5219\u6240\u4ee5\u706f\u90fd\u7528\u8fc7\u4e86\uff0c\u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\u4e2d\n                result.append(\"{}:{:0&gt;2d}\".format(hour, minute))\n\n            for h in range(which, 4):   # \u5c0f\u65f6\u706f\u5e8f\u53f7 0~3\n                if h not in hour_seen:  # \u5f53\u524d\u706f\u5e8f\u53f7\u672a\u4f7f\u7528\u8fc7\n                    hour_seen.add(h)    # \u8bb0\u5f55\n                    backstrace(num-1, hour+ int(pow(2, h)),minute, h+1) # \u5728\u5c0f\u65f6\u706f\u8303\u56f4 \u4f7f\u7528\u4e0b\u4e00\u4e2a\u4eae\u7740\u7684\u706f\uff0c num-1, hour\u589e\u52a0\n                    hour_seen.remove(h)\n\n            for m in range(max(which, 4), 10):  # \u5206\u949f\u706f\uff0c\u8303\u56f44~9\n                if m not in minute_seen:\n                    minute_seen.add(m)\n                    backstrace(num-1, hour, minute + int(pow(2, m-4)), m+1)\n                    minute_seen.remove(m)\n\n        backstrace(num, 0, 0, 0)\n\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c\u8fdb\u5236\u624b\u8868\u9876\u90e8\u6709 4 \u4e2a LED \u4ee3\u8868 \u5c0f\u65f6\uff080-11\uff09\uff0c\u5e95\u90e8\u7684 6 \u4e2a LED \u4ee3\u8868 \u5206\u949f\uff080-59\uff09\u3002 \u6bcf&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/343"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions"}],"predecessor-version":[{"id":344,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions\/344"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}