N-Queens II 解答
Question
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
Solution
This problem seems like 2D DP, but it is not.
For DP problem, usually we will have a/ some directions. But here, we can go to all four directions. So this can not be solved by DP.
We still use the way to implement N-Queens.
There is one stuff worth mentioning:
In java, if in function f(x), we pass a variable x of primitive data type like int into a function g(x), the value of x in f(x) is not changed.
If we want it to be modified, we need to pass a reference.
public class Solution { public int totalNQueens(int n) {
// Should use an object rather than a int variable here
// We want "result" to be modified in helper function
int[] result = {0};
int[] queen = new int[n];
helper(n, 0, queen, result);
return result[0];
} private void helper(int n, int rowNum, int[] queen, int[] result) {
if (n == rowNum) {
result[0]++;
return;
} for (int i = 0; i < n; i++) {
queen[rowNum] = i;
if (check(rowNum, queen))
helper(n, rowNum + 1, queen, result);
}
} private boolean check(int rowNum, int[] queen) {
int colNum = queen[rowNum];
for (int i = 0; i < rowNum; i++) {
if ((colNum == queen[i]) || (queen[i] - i == colNum - rowNum) || (queen[i] + i == colNum + rowNum))
return false;
}
return true;
}
}
N-Queens II 解答的更多相关文章
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- Ugly Number II 解答
Question Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime ...
- Paint House II 解答
Question There are a row of n houses, each house can be painted with one of the k colors. The cost o ...
- Populating Next Right Pointers in Each Node II 解答
Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...
- Word Break II 解答
Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...
- Reverse Linked List II 解答
Question Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Giv ...
- Majority Element II 解答
Question Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Th ...
随机推荐
- 【BBST 之伸展树 (Splay Tree)】
最近“hiho一下”出了平衡树专题,这周的Splay一直出现RE,应该删除操作指针没处理好,还没找出原因. 不过其他操作运行正常,尝试用它写了一道之前用set做的平衡树的题http://codefor ...
- dp优化
入口 A(fzu 1894) 普通的单调队列,trick是进队判断的符号选取(>=wa , >ac). B(poj 2823) 没什么好说的 ,坑爹poj g++,tle ;c++,ac. ...
- GridView视图
本文实现如下效果 Test_Grid.java public class Test_Grid extends Activity { private GridView gridview; private ...
- ios 读取通讯录数据
#import <Foundation/Foundation.h> @interface LoadingContactData : NSObject // 读取通讯录 + (Loading ...
- High bridge, low bridge(离散化, 前缀和)
High bridge, low bridge Q:There are one high bridge and one low bridge across the river. The river h ...
- [CSS] CSS Transitions: Delays and Multiple Properties
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
- C#基础学习心得(一)
类的成员 数据成员:字段,常量(const) 函数成员:方法,属性,索引器,构造函数,析构函数,事件 类的声明 实例成员:对象相关性,不同于同一类的其他实例 静态成员:常量,static修饰的字段,方 ...
- textarea 的最大高度以及最小高度
<script type="text/javascript"> $(function(){ $("#textarea3").textareaAuto ...
- jQuery 双击事件(dblclick)时,不触发单击事件(click)
我这是转载的文字 原文地址:http://www.cnblogs.com/wyblog/archive/2011/12/15/2289219.html 万恶的双击事件啊!! 在jQuery的事件绑定中 ...
- 一个简单方法:构造xml的document,并将其转换为string
首先,构造一个document对象: Document doc = null; try { doc = DocumentBuilderFactory.newInstance() .newDocumen ...