#51

N-Queens

specialist · 925 · lc hard +32 · verified · 75% accepted · 14,213 likes · top 86%

play →

Description

Place n non-attacking queens on an n x n chessboard and return all distinct valid board layouts, using 'Q' for a queen and '.' for an empty cell.

Example 1:

Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above

Example 2:

Input: n = 1
Output: [["Q"]]

Code

1
2
3