{"id":319,"date":"2020-09-26T10:25:25","date_gmt":"2020-09-26T02:25:25","guid":{"rendered":"http:\/\/39.96.58.60\/?p=319"},"modified":"2022-10-18T16:39:50","modified_gmt":"2022-10-18T08:39:50","slug":"leetcode-526-%e4%bc%98%e7%be%8e%e7%9a%84%e6%8e%92%e5%88%97","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=319","title":{"rendered":"Leetcode 526. \u4f18\u7f8e\u7684\u6392\u5217"},"content":{"rendered":"<p>\u5047\u8bbe\u6709\u4ece 1 \u5230 N \u7684&nbsp;<strong>N&nbsp;<\/strong>\u4e2a\u6574\u6570\uff0c\u5982\u679c\u4ece\u8fd9&nbsp;<strong>N&nbsp;<\/strong>\u4e2a\u6570\u5b57\u4e2d\u6210\u529f\u6784\u9020\u51fa\u4e00\u4e2a\u6570\u7ec4\uff0c\u4f7f\u5f97\u6570\u7ec4\u7684\u7b2c <strong>i<\/strong>&nbsp;\u4f4d (1 &lt;= i &lt;= N) \u6ee1\u8db3\u5982\u4e0b\u4e24\u4e2a\u6761\u4ef6\u4e2d\u7684\u4e00\u4e2a\uff0c\u6211\u4eec\u5c31\u79f0\u8fd9\u4e2a\u6570\u7ec4\u4e3a\u4e00\u4e2a\u4f18\u7f8e\u7684\u6392\u5217\u3002\u6761\u4ef6\uff1a<\/p>\n<ol>\n<li>\u7b2c&nbsp;<strong>i&nbsp;<\/strong>\u4f4d\u7684\u6570\u5b57\u80fd\u88ab&nbsp;<strong>i&nbsp;<\/strong>\u6574\u9664<\/li>\n<li><strong>i<\/strong> \u80fd\u88ab\u7b2c <strong>i<\/strong> \u4f4d\u4e0a\u7684\u6570\u5b57\u6574\u9664<\/li>\n<\/ol>\n<p>\u73b0\u5728\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570 N\uff0c\u8bf7\u95ee\u53ef\u4ee5\u6784\u9020\u591a\u5c11\u4e2a\u4f18\u7f8e\u7684\u6392\u5217\uff1f<\/p>\n<p><strong>\u793a\u4f8b1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 2\n\n<strong>\u8f93\u51fa:<\/strong> 2\n\n<strong>\u89e3\u91ca:<\/strong> \n\n\n\n\u7b2c 1 \u4e2a\u4f18\u7f8e\u7684\u6392\u5217\u662f [1, 2]:\n\n  \u7b2c 1 \u4e2a\u4f4d\u7f6e\uff08i=1\uff09\u4e0a\u7684\u6570\u5b57\u662f1\uff0c1\u80fd\u88ab i\uff08i=1\uff09\u6574\u9664\n\n  \u7b2c 2 \u4e2a\u4f4d\u7f6e\uff08i=2\uff09\u4e0a\u7684\u6570\u5b57\u662f2\uff0c2\u80fd\u88ab i\uff08i=2\uff09\u6574\u9664\n\n\n\n\u7b2c 2 \u4e2a\u4f18\u7f8e\u7684\u6392\u5217\u662f [2, 1]:\n\n  \u7b2c 1 \u4e2a\u4f4d\u7f6e\uff08i=1\uff09\u4e0a\u7684\u6570\u5b57\u662f2\uff0c2\u80fd\u88ab i\uff08i=1\uff09\u6574\u9664\n\n  \u7b2c 2 \u4e2a\u4f4d\u7f6e\uff08i=2\uff09\u4e0a\u7684\u6570\u5b57\u662f1\uff0ci\uff08i=2\uff09\u80fd\u88ab 1 \u6574\u9664\n\n<\/pre>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ol>\n<li><strong>N<\/strong> \u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u5e76\u4e14\u4e0d\u4f1a\u8d85\u8fc715\u3002<\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \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\uff1a3088 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.31% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a21.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.19% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u51fa\u7248\uff0c\u4f7f\u7528current\u5b58\u653e\u5f53\u524d\u5217\u8868\uff0c\u901a\u8fc7len(current)\u83b7\u53d6\u5373\u5c06\u63d2\u5165\u7684\u4e0b\u6807\n\"\"\"\nclass Solution:\n    def countArrangement(self, N: int) -&gt; int:\n        nums = list(range(1, N+1))\n        result = []\n\n        def backtrack(current):\n            # print(current)\n            if len(current) == N:\n                result.append(current[:])\n                return\n\n            for i in range(N):\n                if nums[i] not in current and (nums[i] % (len(current)+1) == 0  or (len(current)+1) % nums[i] == 0):\n                    current.append(nums[i])\n                    backtrack(current)\n                    current.pop()\n\n        backtrack([])\n        return len(result)\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a2536 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e869.78% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a21.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.19% \u7684\u7528\u6237\n\n\u4f18\u5316\u601d\u8def\uff1a\n    \u76f4\u63a5\u4ecenums\u4e2d\u53d6\u6570\u5b57\uff0c\u53ef\u4ee5\u63d0\u5347\u4e00\u90e8\u5206\n\"\"\"\nclass Solution:\n    def countArrangement(self, N: int) -&gt; int:\n        nums = list(range(1, N+1))\n        result = []\n\n        def backtrack(current):\n            if len(current) == N:\n                result.append(current[:])\n                return\n\n            for num in nums:\n                if num not in current and (num % (len(current)+1) == 0  or (len(current)+1) % num == 0):\n                    current.append(num)\n                    backtrack(current)\n                    current.pop()\n\n        backtrack([])\n        return len(result)\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a2240 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8615.64% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a21.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.19% \u7684\u7528\u6237\n\n\u4f18\u5316\u601d\u8def\uff1a\n    \u4f7f\u7528\u4e00\u4e2a\u5355\u72ec\u7684index\u53d8\u91cf\uff0c\u8bb0\u5f55\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u3002\n\"\"\"\nclass Solution:\n    def countArrangement(self, N: int) -&gt; int:\n        nums = list(range(1, N+1))\n        result = []\n\n        def backtrack(current, index):\n            if len(current) == N:\n                result.append(current[:])\n                return\n\n            for num in nums:\n                if num not in current and (num % index == 0  or index % num == 0):\n                    current.append(num)\n                    backtrack(current, index+1)\n                    current.pop()\n\n        backtrack([], 1)\n        return len(result)\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5047\u8bbe\u6709\u4ece 1 \u5230 N \u7684&nbsp;N&nbsp;\u4e2a\u6574\u6570\uff0c\u5982\u679c\u4ece\u8fd9&nbsp;N&nbsp;\u4e2a\u6570\u5b57\u4e2d\u6210\u529f\u6784\u9020\u51fa&#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\/319"}],"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=319"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/319\/revisions"}],"predecessor-version":[{"id":320,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/319\/revisions\/320"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}