-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathleetcode_0052_N-Queens_II.java
More file actions
72 lines (65 loc) · 2.23 KB
/
leetcode_0052_N-Queens_II.java
File metadata and controls
72 lines (65 loc) · 2.23 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// AC: Runtime: 3 ms, faster than 54.22% of Java online submissions for N-Queens II.
// Memory Usage: 38.3 MB, less than 15.33% of Java online submissions for N-Queens II.
// backtracking
// T:O(n^3), S:O(n^2)
//
class Solution {
public int totalNQueens(int n) {
List<List<Integer>> record = new LinkedList<>();
int[][] chessboard = new int[n][n];
backtracking(chessboard, new LinkedList<>(), record, 0);
return record.size();
}
private void backtracking(int[][] chessboard, List<Integer> path, List<List<Integer>> out, int startIndex) {
int size = chessboard.length;
List<Integer> pathCopy = new LinkedList<>(path);
if (pathCopy.size() == size) {
out.add(pathCopy);
return;
}
for (int i = 0; i < size; i++) {
if (checkChessboard(chessboard, startIndex, i)) {
pathCopy.add(i);
chessboard[startIndex][i] = 1;
backtracking(chessboard, pathCopy, out, startIndex + 1);
pathCopy.remove(pathCopy.size() - 1);
chessboard[startIndex][i] = 0;
}
}
}
private boolean checkChessboard(int[][] chessboard, int row, int col) {
int size = chessboard.length;
// check column
for (int i = 0; i < size; i++) {
if (i == row) {
continue;
}
if (chessboard[i][col] == 1) {
return false;
}
}
// check 45° diagonal
for (int i = row - 1, j = col + 1; i >= 0 && j < size; i--, j++) {
if (chessboard[i][j] == 1) {
return false;
}
}
for (int i = row + 1, j = col - 1; i < size && j >= 0; i++, j--) {
if (chessboard[i][j] == 1) {
return false;
}
}
// check 135° diagonal
for (int i = row + 1, j = col + 1; i < size && j < size; i++, j++) {
if (chessboard[i][j] == 1) {
return false;
}
}
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (chessboard[i][j] == 1) {
return false;
}
}
return true;
}
}