Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
public class Solution {//本题类似于N-Queens,这个更简单一些,只需要求出解法的个数即可,因此没有了print步骤//注意:isvalid函数的参数,以及函数中i的范围// A[]的意义,可以代表棋盘的行int res;int A[];public int totalNQueens(int n) {A=new int[n];nqueens(0,n);return res;}public void nqueens(int cur,int n){if(cur==n){res++;}else{for(int i=0;i<n;i++){A[cur]=i;if(isValid(cur)){nqueens(cur+1,n); }}}}public boolean isValid(int cur){for(int i=0;i<cur;i++){if(A[i]==A[cur]||Math.abs(A[i]-A[cur])==cur-i)return false;}return true;} }