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: ...
随机推荐
- FastDFS设备、构造、配置()一-安装和部署
FastDFS是一个开源的.高性能的的分布式文件系统,他基本的功能包含:文件存储.同步和訪问,设计基于高可用和负载均衡,FastDFS很适用于基于文件服务的站点.比如图片分享和视频分享站点 FastD ...
- 设计模式模式游客(Visitor)摘录
23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将实例化托付给还 ...
- 《web全栈工程师的自我修养》阅读笔记
在买之前以为这本书是教你怎么去做一个web全栈工程师,以及介绍需要掌握的哪些技术的书,然而看的过程中才发现,是一本方法论的书.读起来的感觉有点像红衣教主的<我的互联网方法论>,以一些自己的 ...
- 数据你把它的金额-JAVA分页
数据量你造吗-JAVA分页 原创地址: http://www.cnblogs.com/Alandre/ (泥沙砖瓦浆木匠),须要转载的,保留下! Thanks 学习的心态第一,解行要对应. 事实 ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
- HDU 2112 HDU Today (Dijkstra算法)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Codeforces 110B-Lucky String(技能)
B. Lucky String time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- HDU2451:Simple Addition Expression
Problem Description A luxury yacht with 100 passengers on board is sailing on the sea in the twiligh ...
- 把自解压的RAR压缩包解压到指定的软件安装目录
原文 把自解压的RAR压缩包解压到指定的软件安装目录 今天千里独行同学给轻狂来信问了一个问题:如何把一个自解压的RAR压缩包解压到我们指定的软件安装目录. 其实,在NSIS中,我们可以灵活运用相关 ...
- SSIS从理论到实战,再到应用(7)----常用的数据类型转换操作
原文:SSIS从理论到实战,再到应用(7)----常用的数据类型转换操作 上期回顾: SSIS从理论到实战,再到应用(6)----SSIS的自带日志功能 在抽取各种应用的数据时候,经常会遇到数据需要转 ...