N-Queens II leetcode java
题目:
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

题解:
这道题跟NQueens的解法完全一样(具体解法参照N QueensN Queens leetcode java),只不过要求的返回值不同了。。所以要记录的result稍微改一下就好了。。。
因为涉及到递归,result传进去引用类型(List,数组之类的)才能在层层递归中得以保存,所以这里使用一个长度为1的数组帮助计数。
当然,也可以使用一个全局变量来帮助计数。
代码如下:
1 public int totalNQueens(int n) {
2 int[] res = {0};
3 if(n<=0)
4 return res[0];
5
6 int [] columnVal = new int[n];
7
8 DFS_helper(n,res,0,columnVal);
9 return res[0];
}
public void DFS_helper(int nQueens, int[] res, int row, int[] columnVal){
if(row == nQueens){
res[0] += 1;
}else{
for(int i = 0; i < nQueens; i++){
columnVal[row] = i;//(row,columnVal[row)==>(row,i)
if(isValid(row,columnVal))
DFS_helper(nQueens, res, row+1, columnVal);
}
}
}
public boolean isValid(int row, int [] columnVal){
for(int i = 0; i < row; i++){
if(columnVal[row] == columnVal[i]
||Math.abs(columnVal[row]-columnVal[i]) == row-i)
return false;
}
return true;
使用全局变量来记录结果的代码是:
1 int res;
2 public int totalNQueens(int n) {
3 res = 0;
4 if(n<=0)
5 return res;
6
7 int [] columnVal = new int[n];
8
9 DFS_helper(n,0,columnVal);
return res;
}
public void DFS_helper(int nQueens, int row, int[] columnVal){
if(row == nQueens){
res += 1;
}else{
for(int i = 0; i < nQueens; i++){
columnVal[row] = i;//(row,columnVal[row)==>(row,i)
if(isValid(row,columnVal))
DFS_helper(nQueens, row+1, columnVal);
}
}
}
public boolean isValid(int row, int [] columnVal){
for(int i = 0; i < row; i++){
if(columnVal[row] == columnVal[i]
||Math.abs(columnVal[row]-columnVal[i]) == row-i)
return false;
}
return true;
}
N-Queens II leetcode java的更多相关文章
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- Palindrome Partitioning II Leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- Permutations II leetcode java
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- Ugly Number II leetcode java
问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...
- Word Ladder II leetcode java
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- Binary Tree Level Order Traversal II leetcode java
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- Remove Duplicates from Sorted Array II leetcode java
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
随机推荐
- 装饰模式和Java IO
装饰模式 修饰模式(装饰模式),是面向对象编程领域中,一种动态地往一个类中添加新的行为的设计模式.就功能而言,修饰模式相比生成子类更为灵活,这样可以给某个对象而不是整个类添加一些功能. 装饰模式的UM ...
- @getMapping和@postMapping,@RestController
@RequestMapping 和 @GetMapping @PostMapping 区别 @GetMapping是一个组合注解,是@RequestMapping(method = Reques ...
- Standard NSD file
%pool: pool=system blockSize=256K layoutMap=cluster allowWriteAffinity=no %pool: pool=datapool block ...
- 02-c#基础之01-基础语法(三)
1.赋值运算符:"=" =:表示赋值的意思,表示把等号右边的值,赋值给等号左边的变量. 由等号连接的表达式称之为赋值表达式. 注意:每个表达式我们都可以求解除一个定值,对于赋值表达 ...
- bzoj4641 基因改造 KMP / hash
依稀记得,$NOIP$之前的我是如此的弱小.... 完全不会$KMP$的写法,只会暴力$hash$.... 大体思路为把一个串的哈希值拆成$26$个字母的位权 即$hash(S) = \sum\lim ...
- [xsy3343]程序锁
题意:有两个序列,序列中数字$\in\{-1,0,1\}$ 有两个指针,初始时分别指向两个序列的开始位置,有一个初始为$0$的数$a$,重复以下过程直到两个指针都指向序列末尾后 如果一个指针指向末尾后 ...
- 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio
题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...
- Spring Boot 运作原理
Spring Boot 运作原理 1.Spring Boot 简介 SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了 ...
- Codeforces Round #283 (Div. 2) C. Removing Columns 暴力
C. Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Python知识(5)--绘图
Python学习变得很方便,不用任何安装一个浏览器也能够使用学习,比如Jupyter就是很好的一个网络工具,提供了编辑编译展示等强大的功能,网址如下: https://try.jupyter.org/ ...