51. N 皇后
题解:
class Solution {
private int n;
private boolean[] col;
private boolean[] master;
private boolean[] slave;
private List<List<String>> res;
public List<List<String>> solveNQueens(int n) {
this.n = n;
col = new boolean[n];
master = new boolean[2 * n - 1];
slave = new boolean[2 * n - 1];
res = new ArrayList<>();
Deque<Integer> stack = new ArrayDeque<>();
dfs(0, stack);
return res;
}
private void dfs(int row, Deque<Integer> stack) {
if (row == n) {
List<String> board = covert2Board(stack, n);
res.add(board);
return;
}
for (int i = 0; i < n; i ++) {
if (!col[i] && !master[row + i] && !slave[row - i + n - 1]) {
stack.addLast(i);
col[i] = true;
master[row + i] = true;
slave[row - i + n - 1] = true;
dfs(row + 1, stack);
col[i] = false;
master[row + i] = false;
slave[row - i + n - 1] = false;
stack.removeLast();
}
}
}
// 保存一个结果的stack,皇后数量:n
private List<String> covert2Board(Deque<Integer> stack, int n) {
List<String> board = new ArrayList<>();
for (Integer num : stack) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i ++) {
sb.append(".");
}
sb.replace(num, num + 1, "Q");
board.add(sb.toString());
}
return board;
}
}
46. 全排列
题解:
class Solution {
public List<List<Integer>> permute(int[] nums) {
Deque<Integer> path = new ArrayDeque<>();
List<List<Integer>> res = new ArrayList<>();
boolean[] visited = new boolean[nums.length];
dfs(nums, -1, visited, path, res);
return res;
}
private void dfs(int[] nums, int index, boolean[] visited, Deque<Integer> path, List<List<Integer>> res) {
final int n = nums.length;
if (path.size() == n) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < n; i ++) {
if (visited[i]) {
continue;
}
path.addLast(nums[i]);
visited[i] = true;
dfs(nums, i, visited, path, res);
visited[i] = false;
path.removeLast();
}
}
}
47. 全排列 II
题解:
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
Deque<Integer> path = new ArrayDeque<>();
List<List<Integer>> res = new ArrayList<>();
boolean[] visited = new boolean[nums.length];
Arrays.sort(nums);
dfs(nums, 0, path, visited, res);
return res;
}
private void dfs(int[] nums, int len, Deque<Integer> path, boolean[] visited, List<List<Integer>> res) {
final int n = nums.length;
if (len == n) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < n; i ++) {
if (visited[i]) {
continue;
}
if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) {
continue;
}
path.addLast(nums[i]);
visited[i] = true;
dfs(nums, len + 1, path, visited, res);
visited[i] = false;
path.removeLast();
}
}
}
37. 解数独
题解:
class Solution {
private int m;
private int n;
private boolean[][] rows;
private boolean[][] cols;
private boolean[][] blocks;
public void solveSudoku(char[][] board) {
this.m = board.length;
this.n = board[0].length;
rows = new boolean[9][10];
cols = new boolean[9][10];
blocks = new boolean[9][10];
for (int i = 0; i < m; i ++) {
for (int j = 0; j < n; j ++) {
if ('.' != board[i][j]) {
char number = board[i][j];
rows[i][number - '0'] = true;
cols[j][number - '0'] = true;
blocks[blockIndex(i, j)][number - '0'] = true;
}
}
}
dfs(board, 0);
}