【leetcode刷题笔记】N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

题解:参见http://www.cnblogs.com/sunshineatnoon/p/3853170.html
只要求求出解的个数,稍微改一下代码就可以了:用私有变量result记录解的数目,当搜索到一个解的时候,result+1即可。
代码如下:
public class Solution {
private int result = 0;
private boolean isValid(List<Integer> cols,int col){
for(int i = 0;i < cols.size();i++){
if(cols.get(i) == col)
return false;
if(cols.size() - i == Math.abs(cols.get(i) - col))
return false;
}
return true;
}
private void QueenDfs(int n,int level,List<Integer> cols){
if(level == n){
result++;
}
for(int i = 0;i < n;i++){
if(isValid(cols, i)){
cols.add(i);
QueenDfs(n, level+1, cols);
cols.remove(cols.size()-1);
}
}
}
public int totalNQueens(int n) {
List<Integer> cols = new ArrayList<Integer>();
QueenDfs(n, 0, cols );
return result;
}
}
【leetcode刷题笔记】N-Queens II的更多相关文章
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode刷题笔记】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode刷题笔记】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- php序列化与反序列化
php序列化简单来说就是 把复杂的数据类型压缩到一个字符串中php对象转换成字符串,反序列化就是把变量转换成对象 一般来说 当把这些序列化的数据放在URL中在页面之间会传递时,需要对这些数据调用ur ...
- Array的push与unshift方法性能比较分析
从原理就可以知道,unshift的效率是较低的.原因是,它每添加一个元素,都要把现有元素往下移一个位置.但到底效率差异有多大呢?下面来测试一下. 测试环境的主要硬件:CPU T7100(1.8G):内 ...
- centos7.0 安装php
1:去php官网下载对应版本的php包 2:解压php包 3:进入解压后的php包 ./configure --with-apxs2=/usr/local/apache2/bin/apxs --wit ...
- 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数
typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...
- 【BZOJ1492】[NOI2007]货币兑换Cash 斜率优化+cdq分治
[BZOJ10492][NOI2007]货币兑换Cash Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下简称B券).每 ...
- [转]Html position(static、relative、absolute、fixed)
转自:http://blog.csdn.net/topviewers/article/details/21644305 讲解不错,转载备忘. position的四个属性值: 1.relative2.a ...
- 【Emit】关于System.MethodAccessException解决方案
最近学习Emit,在使用Emit动态生成对象时碰到一些"蛋疼"的问题,如下: 1.安全透明方法"XXX.XX()"尝试访问安全关键方法"YYY ...
- Java语言实现简单FTP软件------>远程文件管理模块的实现(十)
首先看一下界面: 1.远程FTP服务器端的文件列表的显示 将远程的当前目录下所有文件显示出来,并显示文件的属性包括文件名.大小.日期.通过javax.swing.JTable()来显示具体的数据.更改 ...
- 去掉标题栏/ActionBar后点击menu键时应用崩溃
MainActivity 继承了 ActionBarActivity后,想要去掉标题栏(ActionBar),在程序中加上requestWindowFeature(Window.FEATURE_NO_ ...
- JavaScript测试代码
<!-- 在谷歌浏览器上的console运行 --> //变量 var netPrice = 8.99; alert(netPrice); //字符串方法 var string1 = &q ...