{"id":561,"date":"2020-09-26T10:57:56","date_gmt":"2020-09-26T02:57:56","guid":{"rendered":"http:\/\/39.96.58.60\/?p=561"},"modified":"2022-10-18T16:38:45","modified_gmt":"2022-10-18T08:38:45","slug":"leetcode-52-n%e7%9a%87%e5%90%8e-ii","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=561","title":{"rendered":"Leetcode 52. N\u7687\u540e II"},"content":{"rendered":"<p><em>n&nbsp;<\/em>\u7687\u540e\u95ee\u9898\u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06 <em>n<\/em>&nbsp;\u4e2a\u7687\u540e\u653e\u7f6e\u5728 <em>n<\/em>\u00d7<em>n<\/em> \u7684\u68cb\u76d8\u4e0a\uff0c\u5e76\u4e14\u4f7f\u7687\u540e\u5f7c\u6b64\u4e4b\u95f4\u4e0d\u80fd\u76f8\u4e92\u653b\u51fb\u3002<\/p>\n<p><img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/10\/12\/8-queens.png\" style=\"height: 276px; width: 258px;\"><\/p>\n<p><small>\u4e0a\u56fe\u4e3a 8 \u7687\u540e\u95ee\u9898\u7684\u4e00\u79cd\u89e3\u6cd5\u3002<\/small><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570 <em>n<\/em>\uff0c\u8fd4\u56de <em>n<\/em> \u7687\u540e\u4e0d\u540c\u7684\u89e3\u51b3\u65b9\u6848\u7684\u6570\u91cf\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 4\n\n<strong>\u8f93\u51fa:<\/strong> 2\n\n<strong>\u89e3\u91ca:<\/strong> 4 \u7687\u540e\u95ee\u9898\u5b58\u5728\u5982\u4e0b\u4e24\u4e2a\u4e0d\u540c\u7684\u89e3\u6cd5\u3002\n\n[\n\n&nbsp;[\".Q..\", &nbsp;\/\/ \u89e3\u6cd5 1\n\n&nbsp; \"...Q\",\n\n&nbsp; \"Q...\",\n\n&nbsp; \"..Q.\"],\n\n\n\n&nbsp;[\"..Q.\", &nbsp;\/\/ \u89e3\u6cd5 2\n\n&nbsp; \"Q...\",\n\n&nbsp; \"...Q\",\n\n&nbsp; \".Q..\"]\n\n]\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u7687\u540e<\/strong>\uff0c\u662f<a href=\"https:\/\/baike.baidu.com\/item\/%E5%9B%BD%E9%99%85%E8%B1%A1%E6%A3%8B\" target=\"_blank\" rel=\"noopener noreferrer\">\u56fd\u9645\u8c61\u68cb<\/a>\u4e2d\u7684\u68cb\u5b50\uff0c\u610f\u5473\u7740<a href=\"https:\/\/baike.baidu.com\/item\/%E5%9B%BD%E7%8E%8B\" target=\"_blank\" rel=\"noopener noreferrer\">\u56fd\u738b<\/a>\u7684\u59bb\u5b50\u3002\u7687\u540e\u53ea\u505a\u4e00\u4ef6\u4e8b\uff0c\u90a3\u5c31\u662f\u201c<a href=\"https:\/\/baike.baidu.com\/item\/%E5%90%83%E5%AD%90\" target=\"_blank\" rel=\"noopener noreferrer\">\u5403\u5b50<\/a>\u201d\u3002\u5f53\u5979\u9047\u89c1\u53ef\u4ee5\u5403\u7684\u68cb\u5b50\u65f6\uff0c\u5c31\u8fc5\u901f\u51b2\u4e0a\u53bb\u5403\u6389\u68cb\u5b50\u3002\u5f53\u7136\uff0c\u5979\u6a2a\u3001\u7ad6\u3001\u659c\u90fd\u53ef\u8d70\u4e00\u6216 N-1 \u6b65\uff0c\u53ef\u8fdb\u53ef\u9000\u3002\uff08\u5f15\u7528\u81ea <a href=\"https:\/\/baike.baidu.com\/item\/%E7%9A%87%E5%90%8E\/15860305?fr=aladdin\">\u767e\u5ea6\u767e\u79d1 &#8211; \u7687\u540e<\/a> \uff09<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Hard<\/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\uff1a56 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8680.87% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e868.27% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u540cN\u7687\u540e\u89e3\u9898\u601d\u8def\uff0c\u4f46\u7531\u4e8e\u53ea\u9700\u8981\u8ba1\u7b97\u9898\u89e3\u6570\u91cf\n\"\"\"\nclass Solution:\n    def totalNQueens(self, n: int) -&gt; int:\n        col = []\n        obl1 = []\n        obl2 = []\n        result = [0]\n\n        def backtrack(i:int, current:int):\n            if current == n:\n                result[0] += 1\n                return\n\n            for j in range(n):\n                if j not in col and j-i not in obl1 and j+i not in obl2:\n                    col.append(j)\n                    obl1.append(j-i)\n                    obl2.append(j+i)\n                    current += 1\n                    backtrack(i+1, current)\n                    current -= 1\n                    col.pop()\n                    obl1.pop()\n                    obl2.pop()\n\n        backtrack(0, 0)\n        return result[0]<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>n&nbsp;\u7687\u540e\u95ee\u9898\u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06 n&nbsp;\u4e2a\u7687\u540e\u653e\u7f6e\u5728 n\u00d7n \u7684\u68cb\u76d8\u4e0a\uff0c\u5e76\u4e14\u4f7f\u7687\u540e\u5f7c\u6b64\u4e4b\u95f4\u4e0d\u80fd\u76f8\u4e92&#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\/561"}],"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=561"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions"}],"predecessor-version":[{"id":562,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions\/562"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}