{"id":563,"date":"2020-09-26T10:58:06","date_gmt":"2020-09-26T02:58:06","guid":{"rendered":"http:\/\/39.96.58.60\/?p=563"},"modified":"2022-10-18T16:38:45","modified_gmt":"2022-10-18T08:38:45","slug":"leetcode-996-%e6%ad%a3%e6%96%b9%e5%bd%a2%e6%95%b0%e7%bb%84%e7%9a%84%e6%95%b0%e7%9b%ae","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=563","title":{"rendered":"Leetcode 996. \u6b63\u65b9\u5f62\u6570\u7ec4\u7684\u6570\u76ee"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4&nbsp;<code>A<\/code>\uff0c\u5982\u679c\u8be5\u6570\u7ec4\u6bcf\u5bf9\u76f8\u90bb\u5143\u7d20\u4e4b\u548c\u662f\u4e00\u4e2a\u5b8c\u5168\u5e73\u65b9\u6570\uff0c\u5219\u79f0\u8fd9\u4e00\u6570\u7ec4\u4e3a<em>\u6b63\u65b9\u5f62<\/em>\u6570\u7ec4\u3002<\/p>\n<p>\u8fd4\u56de A \u7684\u6b63\u65b9\u5f62\u6392\u5217\u7684\u6570\u76ee\u3002\u4e24\u4e2a\u6392\u5217 <code>A1<\/code> \u548c <code>A2<\/code> \u4e0d\u540c\u7684\u5145\u8981\u6761\u4ef6\u662f\u5b58\u5728\u67d0\u4e2a\u7d22\u5f15 <code>i<\/code>\uff0c\u4f7f\u5f97 A1[i] != A2[i]\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[1,17,8]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>2\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\n\n[1,8,17] \u548c [17,8,1] \u90fd\u662f\u6709\u6548\u7684\u6392\u5217\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[2,2,2]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>1\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length &lt;= 12<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 1e9<\/code><\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Hard<\/p>\n<p>**\u6807\u7b7e**: \u56fe\u3001 \u6570\u5b66\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\uff1a44 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8673.57% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.4 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8670.71% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf+\u526a\u679d\n\"\"\"\nclass Solution:\n    def numSquarefulPerms(self, A: List[int]) -&gt; int:\n        result = []\n        n = len(A)\n        A.sort()    # \u6392\u5e8f\uff0c\u4fbf\u4e8e\u91cd\u590d\u5143\u7d20\u526a\u679d\n        def backtrack(index, current, used):    # \u5f53\u524d\u8282\u70b9\u7d22\u5f15\uff0c \u5f53\u524d\u5217\u8868\uff0c \u4f7f\u7528\u7528\u7684\u7d22\u5f15\n            if len(current) == n and current not in result: # \u5982\u679c\u5f53\u524d\u5217\u8868\u5305\u542b\u4e86\u6240\u6709A\u5143\u7d20\uff0c\u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\u4e2d\n                result.append(current)\n                return\n\n            for i in range(n):\n                if i not in used:   # \u5faa\u73afA\uff0c\u5bf9\u4e8e\u6ca1\u6709\u4f7f\u7528\u8fc7\u7684\u5143\u7d20\n                    num = (A[index] + A[i]) ** 0.5  # \u8ba1\u7b97\u5f00\u65b9\n                    if i &gt; 0 and A[i] == A[i-1] and i-1 not in used:    # \u91cd\u590d\u5143\u7d20\u526a\u679d\n                        continue\n                    if int(num) == num: # \u5982\u679c\u662f\u5b8c\u5168\u5e73\u65b9\u6570\uff0c\u5219\u5c06A[i]\u6dfb\u52a0\u5230\u5f53\u524d\u5217\u8868\n                        backtrack(i, current+[A[i]], used+[i])  # \u628aindex\u66f4\u65b0\u4e3ai\n\n        for index in range(n):  # \u5df2\u4e0d\u540c\u4e0b\u6807\u5f00\u59cb\n            if index &gt; 0 and A[index] == A[index-1]:    # \u8df3\u8fc7\u76f8\u540c\u5143\u7d20\n                continue\n            backtrack(index, [A[index]], [index])\n\n        return len(result)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4&nbsp;A\uff0c\u5982\u679c\u8be5\u6570\u7ec4\u6bcf\u5bf9\u76f8\u90bb\u5143\u7d20\u4e4b\u548c\u662f\u4e00\u4e2a\u5b8c\u5168\u5e73\u65b9\u6570\uff0c\u5219\u79f0\u8fd9\u4e00\u6570\u7ec4\u4e3a\u6b63\u65b9\u5f62\u6570\u7ec4\u3002 \u8fd4&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","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\/563"}],"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=563"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/563\/revisions"}],"predecessor-version":[{"id":564,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/563\/revisions\/564"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=563"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}