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和本 ...
随机推荐
- ros的源码阅读
测试代码,使用xmlrpc与roscore通信 ros的框架是使用rpc与server端通信,server维护topic的publisher,subscriber,param server,servi ...
- Linq--扩展方法
如果现在有一个这样的需求,求筛选出来的大于20MB的进程的和,常用的方法是写一个静态方法传进去一个ProcessData列表 比如: public static Int64 TotalMemory( ...
- 使用Azure portal Create Virtual Machine
使用简单快速的方式穿件的Virtual Machine 这个步骤隐藏的了很多步骤,例如的创建的云服务(Cloud Service) 创建存储(Storage) 存储名为系统自动产生 可以通过存储看到含 ...
- eclipse安装pydev插件
打开Eclipse,找到Help菜单栏,进入Install New Software…选项. 点击work with:输入框的旁边点击Add…,Name可以随便输入,Location是http://p ...
- MySQL主从修复
MySQL主从故障修复 测试库:192.168.1.2 主192.168.1.3 从 192.168.1.4 主 4又是2的从库192.168.1.5 从 有人修改了192.168.1.2和192.1 ...
- java 接口(基础思想一)
我想,对于各位使用面向对象编程语言的程序员来说,“接口”这个名词一定不陌生,但是不知各位有没有这样的疑惑:接口有什么用途?它和抽象类有什么区别?能不能用抽象类代替接口呢?而且,作为程序员,一定经常听到 ...
- macos port总结
http://stackoverflow.com/questions/733951/unable-to-download-the-source-code-of-open-source-projects ...
- IOS开发问题录:如何在Swift中引入Head文件?
最近在学习IOS开发,从一个简单的登录开始,逐步解决了一个网络访问.获取控件值等问题,遇到了信息加密的问题. 做为IOS的入门者,信息加密需要解决如下几个问题: 1.IOS的MD5加密有没有固定函数, ...
- NodeJS下访问SQL Server
1.下载node-sqlserver (1)msnodesql (msnodesql-0.2.1-v0.8-x64.msi)下载地址:下载 自行选择与自己系统相符的版本,点击安装. (2)msnod ...
- C# type - IsPrimitive
Type t = typeof(string); if (t.IsPrimitive)//not { Console.WriteLine("string is a Primitive&quo ...