52 N-Queens II
52. N-Queens II
1. Question
The n-queens puzzle is the problem of placing n _queens on an _n × _n _chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
2. Implementation
(1) Backtracking
3. Time & Space Complexity
时间复杂度O(n * 2 ^ n), 每一行里只能放一个queen, 每一列只有放和不放两种可能,所以n * 2 ^ n,空间复杂度O(n)
Last updated
Was this helpful?