{"id":2446,"date":"2022-01-27T18:07:12","date_gmt":"2022-01-27T10:07:12","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2446"},"modified":"2022-01-27T18:07:13","modified_gmt":"2022-01-27T10:07:13","slug":"leetcodeday51","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/27\/leetcodeday51\/","title":{"rendered":"leetcodeday51 &#8211;n\u00a0\u7687\u540e"},"content":{"rendered":"\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\u00d7n<\/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\u6240\u6709\u4e0d\u540c\u7684&nbsp;<strong>n\u7687\u540e\u95ee\u9898<\/strong>&nbsp;\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e00\u79cd\u89e3\u6cd5\u5305\u542b\u4e00\u4e2a\u4e0d\u540c\u7684&nbsp;<strong>n \u7687\u540e\u95ee\u9898<\/strong>&nbsp;\u7684\u68cb\u5b50\u653e\u7f6e\u65b9\u6848\uff0c\u8be5\u65b9\u6848\u4e2d&nbsp;<code>'Q'<\/code>&nbsp;\u548c&nbsp;<code>'.'<\/code>&nbsp;\u5206\u522b\u4ee3\u8868\u4e86\u7687\u540e\u548c\u7a7a\u4f4d\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><img alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/13\/queens.jpg\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>n = 4\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;\".Q..\",\"...Q\",\"Q...\",\"..Q.\"],&#91;\"..Q.\",\"Q...\",\"...Q\",\".Q..\"]]\n<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<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>n = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;\"Q\"]]<\/code><\/pre>\n\n\n\n<p>\u56de\u6eaf\u7b97\u6cd5\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<p>\u5728\u5982\u4e0b\u6811\u5f62\u7ed3\u6784\u4e2d\uff1a&nbsp;<img src=\"https:\/\/img-blog.csdnimg.cn\/20210130182532303.jpg\" alt=\"51.N\u7687\u540e\"><\/p>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u9012\u5f52\u5230\u68cb\u76d8\u6700\u5e95\u5c42\uff08\u4e5f\u5c31\u662f\u53f6\u5b50\u8282\u70b9\uff09\u7684\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u6536\u96c6\u7ed3\u679c\u5e76\u8fd4\u56de\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=51 lang=python3\r\n#\r\n# &#91;51] N \u7687\u540e\r\n#\u56de\u6eaf\u7b97\u6cd5 \u4e00\u884c\u4e00\u884c\u8fdb\u884c\u5904\u7406\r\n\r\n# @lc code=start\r\nclass Solution:\r\n    def solveNQueens(self, n: int) -> List&#91;List&#91;str]]:\r\n        if not n: return &#91;]\r\n        board = &#91;&#91;'.'] * n for _ in range(n)]\r\n        res = &#91;]\r\n        def isVaild(board,row, col):\r\n            #\u5224\u65ad\u540c\u4e00\u5217\u662f\u5426\u51b2\u7a81\r\n            for i in range(len(board)):\r\n                if board&#91;i]&#91;col] == 'Q':\r\n                    return False\r\n            # \u5224\u65ad\u5de6\u4e0a\u89d2\u662f\u5426\u51b2\u7a81\r\n            i = row -1\r\n            j = col -1\r\n            while i>=0 and j>=0:\r\n                if board&#91;i]&#91;j] == 'Q':\r\n                    return False\r\n                i -= 1\r\n                j -= 1\r\n            # \u5224\u65ad\u53f3\u4e0a\u89d2\u662f\u5426\u51b2\u7a81\r\n            i = row - 1\r\n            j = col + 1\r\n            while i>=0 and j &lt; len(board):\r\n                if board&#91;i]&#91;j] == 'Q':\r\n                    return False\r\n                i -= 1\r\n                j += 1\r\n            return True\r\n\r\n        def backtracking(board, row, n):\r\n            # \u5982\u679c\u8d70\u5230\u6700\u540e\u4e00\u884c\uff0c\u8bf4\u660e\u5df2\u7ecf\u627e\u5230\u4e00\u4e2a\u89e3\r\n            if row == n:\r\n                temp_res = &#91;]\r\n                for temp in board:\r\n                    temp_str = \"\".join(temp)\r\n                    temp_res.append(temp_str)\r\n                res.append(temp_res)\r\n            for col in range(n):\r\n                if not isVaild(board, row, col):\r\n                    continue\r\n                board&#91;row]&#91;col] = 'Q'\r\n                backtracking(board, row+1, n)\r\n                board&#91;row]&#91;col] = '.'\r\n        backtracking(board, 0, n)\r\n        return res\r\n# @lc code=end\r\n\r\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"781\" height=\"211\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-303.png\" alt=\"\" class=\"wp-image-2447\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-303.png 781w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-303-300x81.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-303-768x207.png 768w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>n&nbsp;\u7687\u540e\u95ee\u9898&nbsp;\u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06&nbsp;n&nbsp;\u4e2a\u7687\u540e\u653e\u7f6e\u5728&nbsp;n\u00d7n&#038;nb &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/27\/leetcodeday51\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday51 &#8211;n\u00a0\u7687\u540e<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2446"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2446"}],"version-history":[{"count":3,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2446\/revisions"}],"predecessor-version":[{"id":2450,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2446\/revisions\/2450"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2446"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}