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.
Example:
Input: 4 Output: 2 Explanation: There are two distinct solutions to the 4-queens puzzle as shown below. [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ]
hard 级别,还是跟上个题目leetcode 51. N-Queens 一样的,但是这个更简单些,只输出个数,不求具体的位置。
稍微改一下上个题的方式,返回结果改为全局的计数变量。
static int res ;
public static int totalNQueens(int n) {
res =0;
int[] queens = new int[n];
//初始化-1
for(int i=0;i
Runtime: 2 ms, faster than 55.53% of Java online submissions for N-Queens II.
Memory Usage: 32.7 MB, less than 13.04% of Java online submissions for N-Queens II.