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: ...
随机推荐
- REQIMPORT-购买内部应用程序(R12.2.3)
採购内部申请(R12.2.3) --US Program:Requisition Import Short Name:REQIMPORT Application:Purchasing Execu ...
- 复制(1)——SQLServer 复制简介
原文:复制(1)--SQLServer 复制简介 前言: SQLServer的复制技术最少从SQLServer2000时代已经出现,当初是为了分布式计算,不是为了高可用.但是到了今天,复制也成为了一种 ...
- url参数中出现+、空格、=、%、&、#等字符的解决办法
url出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好?解决办法将这些字符转化成服务器可以识别的字符,对应关系如下:URL字符转义 用其它 ...
- Android Property Animation 物业动画
效果图: Property Animation介绍: 出生在sdk3.0,是利用了View所拥有的属性,进行一系列的操作. 比方一个View有什么样的setAbc的属性,那么理论上就能够设置它. ...
- Linux下find命令具体解释
1. find命令 linux的find命令用来查找文件,功能非常强大, 能够通过时间, 用户组, 文件名称, 文件类型, 权限,大小等来查找对应文件. 2. find的使用方法 通过find --h ...
- HDU 2112 HDU Today (Dijkstra算法)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 高通公司 MSM8K GPT异常原因分析无法开机的问题
问题分析过程如下面: 一. MSM8916台gpt概率问题:采用QPST emmc software download下载软件工具后,无法开机.例如下面的附图: log分析是userdata分区未成功 ...
- JS日期显示格式 yyyy-MM-dd hh:mm:ss
1.字符串转换为日期 Date.parse() 可以把 Date.toString() 和 Date.toUTCString()返回的字符串转换为日期类型 2.日期对象转换 ...
- ECLIPSE IDEA 调音 1
为自己所用IDE进行jvm优 首先进行日志输出配置 Eclipse 改动eclipse.ini IDEA 改动 idea.exe.vmoptions 添加打印日志的配置參数 -XX:+Print ...
- CentOS6.5查看一port执行状态
netstat -nap | grep 22 版权声明:本文博主原创文章,博客,未经同意不得转载.