LeetCode——N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

原题链接:https://oj.leetcode.com/problems/n-queens-ii/
题目:求有多少个独立的解决方式。
与上题一致,仅仅要每次成功后记录一下次数就可以。
package leetcode; import java.util.ArrayList;
import java.util.List; public class NQueensII {
public static void main(String[] args) {
System.out.println(new NQueensII().totalNQueens(5));
}
int res = 0;
public int totalNQueens(int n) {
List<String[]> result = new ArrayList<String[]>();
List<Integer> cols = new ArrayList<Integer>();
if(n <= 0)
return 0;
search(result,cols,n);
return res;
}
public void search(List<String[]> result,List<Integer> cols,int n){
if(cols.size() == n){
result.add(draw(cols));
res++;
return;
}
for(int col=0;col<n;col++){
if(!isValid(cols,col))
continue;
cols.add(col);
search(result,cols,n);
cols.remove(cols.size()-1);
}
} public String[] draw(List<Integer> cols){
String[] chess = new String[cols.size()];
for(int i=0;i<chess.length;i++){
chess[i] = "";
for(int j=0;j<cols.size();j++){
if(j==cols.get(i))
chess[i] += "Q";
else
chess[i] += ".";
}
}
return chess;
}
public boolean isValid(List<Integer> cols,int col){
int row = cols.size();
for(int i=0;i<row;i++){
if(cols.get(i) == col)
return false;
if(i - cols.get(i) == row - col)
return false;
if(i+cols.get(i) == row+col)
return false;
}
return true;
}
}
LeetCode——N-Queens II的更多相关文章
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
- [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用
[LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...
- Leetcode | N-Queens I & II
N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
随机推荐
- vmWare虚拟机下ubuntu配置代理上网
最终用上了linux,windows下用vmWare安装虚拟机,挂ubuntu 14.04,,认为非常不习惯,非常难侍候. 而且遇到的客观困难还不少,这两天着实费了不少功夫.主要是上班这里上网要配置代 ...
- 2014年10本月微软MVP应用程序启动!
2014年10本月微软MVP启动应用程序! CSDN与微软合作,长期为用户提供申请"微软最有价值专家"的平台,希望有兴趣.资历的朋友以及正在朝这个方向努力的朋友可以积极參与 ...
- 【Swift】学习笔记(四)——设置(Collection)
Swift和其他语言也提供了两种类型的集合:数组和字典 数组:数组用来按顺序存储同样类型的数据,swift规定它是类型安全的,每个数组都有自己的类型也就是其它语言所说的泛型. 创建数组: 1.var ...
- Effective C++:规定27:尽量少做动作的过渡
(一个)C风格遗留转换: (T)expression T(expression) (二)C++提供四种新式转型: (1)const_cast<T>(expression):去除表达式的常量 ...
- 左右margin top问题百分比值
不想说今天心情有多差! 9点多開始发现一个"bug",需求是依据屏幕高度动态调整某个div的位置.代码大概是这种. <div style="margin-top: ...
- eclipse android ndk 提示Type 'JNIEnv' could not be resolved 等信息解决办法
新配置完eclipse c++ android ndk 环境后,导入项目提示以下信息 是由于没有将jni.h导入的缘故,而这个文件在ndk的目录下面.所以,参照以下步骤:Project Propert ...
- SQL入门学习3-数据更新
4-1 数据的插入(INSERT语句的使用方法) 使用INSERT语句可以向表中插入数据(行).原则上,INSERT语句背刺执行一行数据插入. CREATE TABLE 和INSERT 语句,都可以设 ...
- SharePoint 2013 母版页修改后,无法添加应用程序
原文:SharePoint 2013 母版页修改后,无法添加应用程序 问题描述:前一段时间尝试了一下将HTML文件转换为母版页,但是,用着用着又发现新的问题,我们转换的母版页,设置成默认母版页以后,无 ...
- docker搭建基础的tomcat应用
tomcat server是眼下比較流行的开源中间件server,以下介绍怎样使用 docker 来做一个 tomcat 数据库服务.官方的仓里没有标 OFFICIAL 的 tomcat 的镜像,只是 ...
- 具体分析Struts工作流程
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXV3ZW56aGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...