题目:

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

Show Tags
Show Similar Problems
 

链接: http://leetcode.com/problems/n-queens-ii/

题解:

刚昨晚NQueens I, 这换个问题又算一题...不过省了我们很多事。只需要设一个global的count来记录找到解的数目。

Time Complexity - O(nn), Space Complexity - O(n)

public class Solution {
private int count = 0; public int totalNQueens(int n) {
if(n <= 0)
return count;
int[] queens = new int[n]; // queens must be a permutation of n
trySolveNQueens(queens, 0);
return count;
} private void trySolveNQueens(int[] queens, int pos) {
int len = queens.length;
if(pos == len)
count++;
else {
for(int i = 0; i < len; i++) {
queens[pos] = i;
if(isBoardValid(queens, pos))
trySolveNQueens(queens, pos + 1);
}
}
} private boolean isBoardValid(int[] queens, int pos) {
for(int i = 0; i < pos; i++) {
if(queens[i] == queens[pos]) // column conflicts
return false;
else if(Math.abs(queens[pos] - queens[i]) == Math.abs(i - pos)) // diagonal conflicts
return false;
} return true;
}
}

二刷:

Java:

上一题的简化版本,只需要求有多少种solution就可以了。以后再了解一下NQueens的fancy做法

Time Complexity - O(nn), Space Complexity - O(n)

public class Solution {
private int totalQueens = 0;
public int totalNQueens(int n) {
if (n <= 0) {
return 0;
}
int[] queens = new int[n];
trySolveNQueens(queens, 0);
return totalQueens;
} private void trySolveNQueens(int[] queens, int pos) {
if (pos == queens.length) {
totalQueens++;
} else {
for (int i = 0; i < queens.length; i++) {
queens[pos] = i;
if (isBoardValid(queens, pos)) {
trySolveNQueens(queens, pos + 1);
}
}
}
} private boolean isBoardValid(int[] queens, int pos) {
for (int i = 0; i < pos; i++) {
if (queens[i] == queens[pos]) {
return false;
}
if (Math.abs(queens[i] - queens[pos]) == Math.abs(i - pos)) {
return false;
}
}
return true;
}
}

Reference:

52. N-Queens II的更多相关文章

  1. Leetcode之回溯法专题-52. N皇后 II(N-Queens II)

    Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...

  2. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...

  3. leetcode 51. N皇后 及 52.N皇后 II

    51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...

  4. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

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

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

  6. LeetCode(52) N-Queens II

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

  7. [LeetCode] 52. N皇后 II

    题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...

  8. leedcode算法解题思路

    1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...

  9. Leetcode-剪枝

    51. N皇后 https://leetcode-cn.com/problems/n-queens/ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. ...

  10. HDU 1852 Beijing 2008 数论

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1852 这道题和HDU1452类似. 题意:给你一个n.k,让你求2008^n所有因子的和(包括1和本 ...

随机推荐

  1. 对现有Hive的大表进行动态分区

    分区是在处理大型事实表时常用的方法.分区的好处在于缩小查询扫描范围,从而提高速度.分区分为两种:静态分区static partition和动态分区dynamic partition.静态分区和动态分区 ...

  2. mapreduce 实现pagerank

    输入格式: A 1 B,C,D B 1 C,Dmap: B A 1/3 C A 1/3 D A 1/3 A |B,C,D C B 1/2 D B 1/2 B |C,Dreduce: B (1-0.85 ...

  3. python之super()函数

    python之super()函数 python的构造器奇特, 使用魔方. 构造器内对基类对象的初始化同样也很奇特, 奇特到没有半点优雅! 在构造器中使用super(class, instance)返回 ...

  4. [ios]ios的延迟执行方法

    1.最直接的方法performSelector:withObject:afterDelay: 这种方法的缺点:每次要为延时写一个方法   2.使用类别,用BOLCK执行 [代码]c#/cpp/oc代码 ...

  5. PHP dirname() 函数 __FILE__ __DIR__

    __DIR__返回文件所处的目录,除非是根目录,否则末尾不带\ __FILE__ 返回文件路径 dirname(__DIR__) 文件所处目录的上级目录,末尾也不带斜杠

  6. 解决VS2012新建MVC3等项目时,收到加载程序集“NuGet.VisualStudio.Interop…”的错误

    vs2012来做一个mvc3的项目,哪知在创建ado数据模型时跳出这么一个东东 错 误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0 ...

  7. (菜鸟要飞系列)一,基于Asp.Net MVC5的后台管理系统(前言)

    今天真是个郁闷的日子,因为老师两个星期前给我的一个任务,用递归算法将Oracle数据库中用户信息及权限显示在jquery-treeView上,网上虽然有大神写出了这类算法,但是不贴全部代码,真的很难跟 ...

  8. Daject初探 - 一个开源关系型数据库对象关系映射(ORM)模型

    Daject简介 Daject是用php写的一个关系型数据库抽象模型,通过该模型,可以在不写任何SQL或写很少的SQL就能执行大多数数据库查询操作.Daject具有面向对象,跨数据库的优点,通过数据库 ...

  9. layoutSubviews -- setNeedsLayout -- layoutIfNeeded -- 区别

    -layoutSubviews方法:这个方法,默认没有做任何事情,需要子类进行重写-setNeedsLayout方法: 标记为需要重新布局,异步调用layoutIfNeeded刷新布局,不立即刷新,但 ...

  10. phpcms v9 企业黄页加入企业会员提示“请选择企业库类型!”

    很多新进站长选择使用了PHPCMS v9内容管理系统,它强大的功能无疑吸引了很多人,尤其是企业黄页功能更是其中的佼佼者,但是官方发布的版本兼容性比较差,出现会员加入企业会员提示“请选择企业库类型!”, ...