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 ...
随机推荐
- 利用过滤器对string类型的入参进行统一trim
背景 最近做的一些项目都是后台管理系统,主要是对表单数据的增删改查操作,其中有些表单项是字符串类型的,对于这些类型的表单项就需要在保存或编辑之前要进行.trim()处理,刚开始感觉没什么,遇到了就手动 ...
- Java 中线程安全问题
不好意思,一个国庆假期给我放的都不知道东西南北了,放松,很放松,差一点就弃更了,感谢那些催更的小伙伴们! 虽然没有更新,但是日常的学习还是有的,以后我尽量给大家分享一些通用知识,非技术. 但是本期还是 ...
- JAVAEE——SSH项目实战05:用户注册、登陆校验拦截器、员工拜访客户功能和MD5加密
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7170519.html 一.用户注册 显示错误信息到页面上的另一种方法: public ...
- CentOS通过光盘启动救援数据
(1).CentOS6 1)首先确保实体机有光盘,虚拟机有光盘镜像.并通过BIOS设置从光盘启动,实体机请通过提示进入BIOS,虚拟机请找到上方菜单中虚拟机-->电源-->打开电源时进入固 ...
- CSS3组件化之ios版菊花loading
<div class="juhua-loading"> <div class="jh-circle1 jh-circle-ios">&l ...
- python2.7 关于打印中文的各种方法
目录 str类型的中文 第一种姿势:逐个打印 第二种姿势: json dumps 第三种姿势: repr string_escape 第四种姿势:PEP3140 unicode类型的中文 当str与u ...
- pip作用
介绍 pip 是一个安装和管理 Python 包的工具,python安装包的工具有easy_install, setuptools, pip,distribute.使用这些工具都能下载并安装djang ...
- Spring之Spring环境搭建
Spring之Spring环境搭建 一.什么是Spring? Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spr ...
- SpringBoot静态资源目录
在web开发中,静态资源的访问是必不可少的,如:图片.js.css 等资源的访问. SpringBoot对静态资源访问提供了很好的支持,基本使用默认配置就能满足开发需求. 在传统的web项目中静态资源 ...
- hdu 3949 第k大异或组合
题意: 给你一些数,其中任选一些数(大于等于一个),那么他们有一个异或和. 求所有这样的异或和的第k小. 我们可以将每一位看成一维,然后就是给我们n个60维的向量,求它们线性组合后得到的向量空间中,第 ...