{"id":242,"date":"2024-12-02T14:56:17","date_gmt":"2024-12-02T06:56:17","guid":{"rendered":"http:\/\/getzs.online\/?p=242"},"modified":"2024-12-02T14:56:17","modified_gmt":"2024-12-02T06:56:17","slug":"leetcode-%e6%af%8f%e6%97%a5%e4%b8%80%e9%a2%98-24-12-2","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2024\/12\/02\/leetcode-%e6%af%8f%e6%97%a5%e4%b8%80%e9%a2%98-24-12-2\/","title":{"rendered":"LeetCode \u6bcf\u65e5\u4e00\u9898 24.12.2"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/n-queens-ii\/\">52. N \u7687\u540e II<\/a><\/p>\n\n\n\n<p><strong>n&nbsp;\u7687\u540e\u95ee\u9898<\/strong>&nbsp;\u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06&nbsp;<code>n<\/code>&nbsp;\u4e2a\u7687\u540e\u653e\u7f6e\u5728&nbsp;<code>n \u00d7 n<\/code>&nbsp;\u7684\u68cb\u76d8\u4e0a\uff0c\u5e76\u4e14\u4f7f\u7687\u540e\u5f7c\u6b64\u4e4b\u95f4\u4e0d\u80fd\u76f8\u4e92\u653b\u51fb\u3002<\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570&nbsp;<code>n<\/code>&nbsp;\uff0c\u8fd4\u56de&nbsp;<strong>n \u7687\u540e\u95ee\u9898<\/strong>&nbsp;\u4e0d\u540c\u7684\u89e3\u51b3\u65b9\u6848\u7684\u6570\u91cf\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/13\/queens.jpg\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>n = 4<br><strong>\u8f93\u51fa\uff1a<\/strong>2<br><strong>\u89e3\u91ca\uff1a<\/strong>\u5982\u4e0a\u56fe\u6240\u793a\uff0c4 \u7687\u540e\u95ee\u9898\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u89e3\u6cd5\u3002<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>n = 1<br><strong>\u8f93\u51fa\uff1a<\/strong>1<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= n &lt;= 9<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u56de\u6eaf\uff1a<\/h2>\n\n\n\n<p>\u5229\u7528\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u5224\u65ad\u5355\u4e2a\u70b9\u662f\u5426\u7b26\u5408\u6761\u4ef6\uff0c\u7b26\u5408\u518d\u9012\u5f52\u4e0b\u53bb\uff0c\u5982\u679c\u6700\u540e\u4e00\u4e2a\u90fd\u6ee1\u8db3\uff0c\u5219\u7b97\u4e00\u79cd\u7ed3\u679c\u3002\u590d\u6742\u5ea6O(N!)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    static int&#91;] chess;\n\n    public static int totalNQueens(int n) {\n        chess = new int&#91;n];\n        return dfs(0, n);\n    }\n\n    public static int dfs(int floor, int n) {\n        if (floor == n) {\n            return 1;\n        }\n        int sum = 0;\n        for (int i = 0 ; i &lt; n ; i++) {\n            boolean flag = true;\n            for (int j = 0 ; j &lt; floor ; j++) {\n                if (chess&#91;j] == i || Math.abs(chess&#91;j] - i) == Math.abs(j - floor)) {\n                    flag = false;\n                    break;\n                }\n            }\n            if (flag) {\n                chess&#91;floor] = i;\n                sum += dfs(floor + 1, n);\n            }\n        }\n        return sum;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>52. N \u7687\u540e II n&nbsp;\u7687\u540e\u95ee\u9898&nbsp;\u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06&nbsp;n&nbsp;\u4e2a\u7687\u540e\u653e\u7f6e\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[5],"class_list":["post-242","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-leetcode-"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=242"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":243,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/242\/revisions\/243"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}