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的更多相关文章

  1. [Leetcode] n queens ii n皇后问题

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  2. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  4. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  5. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  6. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

  7. [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用

    [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...

  8. 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 ...

  9. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  10. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

随机推荐

  1. C# 经典排序算法大全

    C# 经典排序算法大全 选择排序 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  2. aauto攫http数据

    说明:灵巧使用上述数据的抓取网页简单的例子. 样例:想把某站点的数据抓回来.然后保存在数据库里边. 步骤:1.打开sql.new一个数据库Test.新建一个表test. 2.打开快手.准备抓数据,以这 ...

  3. POJ3581:Sequence(后缀数组)

    Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to ...

  4. Effective C++:规定27:尽量少做动作的过渡

    (一个)C风格遗留转换: (T)expression T(expression) (二)C++提供四种新式转型: (1)const_cast<T>(expression):去除表达式的常量 ...

  5. unity3d 各功能的运行秩序,打回来,订购,的次数

    Update 当MonoBehaviour启用时,其Update在每一帧被调用. 仅调用一次(每帧) LateUpdate 当Behaviour启用时,  每帧调用一次: FixedUpdate 当M ...

  6. mysql数据文件迁移到新的硬盘分区的方法

    该系统增加了一个硬盘.要创建新的分区/data文件夹,mysql对于数据文件夹/var/lib/mysql 1.  停止mysql维修 [root@localhost~]# service mysql ...

  7. 用户配置文件(passwd/shadow)

    管理员工作,这是管理帐户的一个非常重要的组成部分.由于整个系统你在的管理, 和所有一般 郄用户帐号申请.所有的,他们会通过你的工作需要得到援助.所以,你需要知道他将如何管理服务器主机挈朋友 帐号! 在 ...

  8. Nginx + IIS 配置,实现负载均衡

    当你的Web应用程序访问量大的时候,一台服务器可能会因为压力过大而无法处理所有的请求.此时,可以增加服务器,采用负载均衡来分担所有的请求.关于Nginx的作用,自行百度了解.总之,在Windows平台 ...

  9. 使用一个T-SQL语句批量查询数据表占用空间及其行数

    原文:使用一个T-SQL语句批量查询数据表占用空间及其行数 要找到数据库中数据表占用的空间和存在的行数.可以使用sp_spaceused搭配数据表的名称.就可以产生该表耗用的空间和现有行数. 如: U ...

  10. SQL 2008执行语句遇到内存不足(1)——error 701

    原文:SQL 2008执行语句遇到内存不足(1)--error 701 转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/17/sql-2008-e ...