-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
39 lines (34 loc) · 955 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @author changleamazing
* @date 2019/12/18 03:05
* source: https://leetcode.com/problems/n-queens-ii/
**/
public class Solution {
int res;
public int totalNQueens(int n) {
int[] position = new int[n];
place(0, position);
return res;
}
private void place(int rowN, int[] position) {
if (rowN == position.length) {
res++;
return;
}
for (int column = 0; column < position.length; column++) {
position[rowN] = column;
if (judge(rowN, position)) {
place(rowN + 1, position);
}
}
}
private boolean judge(int rowN, int[] position) {
for (int row = 0; row < rowN; row++) {
if (position[row] == position[rowN] || rowN - row == Math
.abs(position[rowN] - position[row])) {
return false;
}
}
return true;
}
}