52. N-Queens II
题目:
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

链接: 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的更多相关文章
- Leetcode之回溯法专题-52. N皇后 II(N-Queens II)
Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- leetcode 51. N皇后 及 52.N皇后 II
51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- LeetCode(52) N-Queens II
题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...
- [LeetCode] 52. N皇后 II
题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...
- leedcode算法解题思路
1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
- Leetcode-剪枝
51. N皇后 https://leetcode-cn.com/problems/n-queens/ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. ...
- HDU 1852 Beijing 2008 数论
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1852 这道题和HDU1452类似. 题意:给你一个n.k,让你求2008^n所有因子的和(包括1和本 ...
随机推荐
- cordova ios
使用Cordova进行iOS开发 (环境配置及基本用法) 字数1426 阅读3044 评论0 喜欢5 安装Cordova CLI 1. cordova的安装: 1.1 安装cordova需要先安装no ...
- C# 反射学习总结
C#中的反射可以使得程序集和类型(类.结构.委托.接口和枚举)以及类型中的成员(方法.字段.属性.事件.参数.构造函数等)都成为变量在编程中动态调用.
- return *this和return this的区别
别跟我说, return *this返回当前对象, return this返回当前对象的地址(指向当前对象的指针). 正确答案为:return *this返回的是当前对象的克隆(当然, 这里仅考虑返回 ...
- android控件之EditText
EditText继承关系:View-->TextView-->EditTextEditText的属性很多,这里介绍几个:android:hint="请输入数字!"//设 ...
- 如何在mysql中退出当前窗口界面而不关闭窗口的方法
CTRL+Cexit;quit ;
- inputstream和outputstream读写数据模板代码
//读写数据模板代码 byte buffer[] = new byte[1024]; int len=0; while((len=in.read(buffer))>0){ out.write(b ...
- 悲惨的Android程序员
Android程序员太悲惨了,连Android官网都访问不了,整个Android程序员的水平都被拉低了一个等级.受不了了.说说悲惨的遭遇吧. 起源:高射炮打苍蝇,驴受伤了 Android一个纯技术网站 ...
- C# Winform 拖放操作
http://www.cnblogs.com/imlions/p/3189773.html 在开发程序的时候,为了提高用户的使用体验,或满足相关用户的功能,总是离不开拖放功能.而本文是总结winfor ...
- 【BZOJ】【3196】Tyvj 1730 二逼平衡树
树套树 Orz zyf 学(co)习(py)了一下树套树的写法,嗯……就是线段树套平衡树. 具体实现思路就是:外部查询用的都是线段树,查询内部自己调用平衡树的操作. 抄抄代码有助理解= = 八中挂了… ...
- spring mvc 数据绑定总结
spring mvc 做web开发时,经常会不知道如何合适绑定页面数据.用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便(本人最开始也是这么认为),经过一段时间的应用 ...