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

  1. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. 【leetcode刷题笔记】Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  3. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  4. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  5. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  6. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  7. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. 【leetcode刷题笔记】Palindrome Partitioning II

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

  9. 【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 ...

随机推荐

  1. No breeds found in the signature, a signature update is recommended

    cobbler 2.6.11 遇到这个问题,需要 >> cobbler signature update >> and cobblerd restart 转自: https:/ ...

  2. PHPstorm如何安装vue.js插件

    1.什么是PHPstorm? PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时错误检查.----来自百度百科 一句话:P ...

  3. android 中使用svg

    http://www.see-source.com/blog/300000038/1189.html http://www.jianshu.com/p/30dfa5920658#

  4. docker centos yum 源

    aliyun yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.rep ...

  5. 遇到IIS configuration error错误的可以看看,不一定是权限问题

    最近接手了别人的一个 DOT NET项目,编译.调试一切都OK(心里暗暗高兴),发布吧,结果放到服务器上一运行出现Configuration Error错误,提示:“Access to the pat ...

  6. Ubuntu中安装jdk环境

    1.Installing default JRE/JDK sudo apt-get update sudo apt-get install default-jre sudo apt-get insta ...

  7. 每天一个Linux命令(27)gzip命令

    zip命令用来压缩文件.gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处“.gz”扩展名.     (1)用法:     用法:  gzip [选项参数][-s <压缩字尾字符 ...

  8. Data Structure Graph: cycle in a directed graph

    geeks上的解答复杂了些,用回溯就行了 #include <iostream> #include <vector> #include <algorithm> #i ...

  9. var妙用

    var广泛使用其实也有用的.比如在一些不太确定类型的地方 (比如要区分int/uint/long/double的时候),用泛型太牛刀而不用又觉得不灵活的时候,其实是比较推荐var的比如设计某种类的时候 ...

  10. castle windsor学习----- Referencing types in XML 在xm文件中引用类型

    当从xml引用installer的语法如下 <install type="Acme.Crm.Infrastructure.ServicesInstaller, Acme.Crm.Inf ...