1 Star 1 Fork 1

Clock966 / LeetcodeEveryday

forked from 四方云和 / LeetcodeEveryday 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
NQueensII.java 798 Bytes
一键复制 编辑 原始数据 按行查看 历史
RunAtWorld 提交于 2020-03-31 00:18 . Java 语言解答 package
package solution;
public class NQueensII {
private int total;
public int totalNQueens(int n) {
int[] f = new int[n];
dfs(f, 0);
return total;
}
private void dfs(int[] f, int row) {
if (row == f.length) {
total++;
return;
}
for (int j = 0; j < f.length; j++) {
if (isValid(f, row, j)) {
f[row] = j;
dfs(f, row + 1);
}
}
}
private boolean isValid(int[] f, int row, int col) {
for (int i = 0; i < row; i++) {
if (f[i] == col) {
return false;
}
if (Math.abs(i - row) == Math.abs(f[i] - col)) {
return false;
}
}
return true;
}
}
1
https://gitee.com/Clock966/LeetcodeEveryday.git
git@gitee.com:Clock966/LeetcodeEveryday.git
Clock966
LeetcodeEveryday
LeetcodeEveryday
master

搜索帮助