Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

The idea is the same as that of Problem N-Queens problem : dfs
public class Solution {
public int totalNQueens(int n) {
List<Integer> solution = new ArrayList<Integer>();
solution.add(0);
if(n == 1) return 1;
if(n >= 4){
List<Integer> position = new ArrayList<Integer>();
dfs(n, 0, position, solution);
}
return solution.get(0);
}
private boolean dfs(int n, int row, List<Integer> position, List<Integer> solution){
if(row == n) return true;
for(int i = 0; i < n; ++i){
if(isValid(n, row * n + i, position)) {
position.add(row * n + i);
if(dfs(n, row + 1, position, solution))
solution.set(0, solution.get(0) + 1);
position.remove(row);
}
}
return false;
}
private boolean isValid(int n, int k, List<Integer> position){
for(int i = 0; i < position.size(); ++i){
if((k % n == position.get(i) % n) || Math.abs(k % n - position.get(i) % n) == (k / n - i))
return false;
}
return true;
}
}