-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy path52.n-queens-ii.java
46 lines (40 loc) · 938 Bytes
/
52.n-queens-ii.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
40
41
42
43
44
45
/*
* @lc app=leetcode id=52 lang=java
*
* [52] N-Queens II
*/
class Solution {
public int totalNQueens(int n) {
int[] result = new int[n];
return total(result, n, 0);
}
public int total(int[] result, int n, int row) {
if (row == n) {
return 1;
}
int count = 0;
for (int column = 0; column < n; column++) {
if (isOk(result, n, row, column)) {
result[row] = column;
count += total(result, n, row + 1);
}
}
return count;
}
public boolean isOk(int[] result, int n, int row, int column) {
int leftup = column - 1;
int rightup = column + 1;
for (int i = row - 1; i >= 0; i--) {
if (result[i] == column) return false;
if (leftup >= 0) {
if (result[i] == leftup) return false;
}
if (rightup < n) {
if (result[i] == rightup) return false;
}
leftup--;
rightup++;
}
return true;
}
}