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 ...
随机推荐
- Zigzag Iterator 解答
Question Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- hdu 5676 ztr loves lucky numbers(dfs+离线)
Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...
- C# 制作 仪表
以前在百度写的文档,转移到此处 前些天在做NetAnalyzer时,需要使用一个指针仪表,网上看了一下,也有人做过,但是大部分都是收费的,本着自力更生的原则,于是决定自己设计一个,今天拿出来有读者分享 ...
- Android学习总结——本地广播机制
为了简单解决广播的安全性问题,Android引入了一套本地广播机制,使用这个机制发出的广播只能在程序的内部进行传递,只能接受来自本应用程序发出的广播.否则当我们发送一些携带关键数据的广播可能被截获,一 ...
- C语言获取系统当前时间转化成时间字符串
因为保存的文件须要加上保存的时间,所以须要一个函数来将系统当前时间获取出来,同一时候转换成时间字符串.详细的时间代码例如以下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- Content-Disposition的使用和注意事项(转载)
Content-Disposition的使用和注意事项 最近不少Web技术圈内的朋友在讨论协议方面的事情,有的说web开发者应该熟悉web相 关的协议,有的则说不用很了解.个人认为这要分层次来看待这个 ...
- eclipse中运行tomcat找不到jre的解决办法
一.在eclipse中选择 window--->preferences 二.runtime environment ----->edit 三.在这个地方就可以进行选择jre了.
- 【转载】NSURLSession教程
原文:http://www.raywenderlich.com/51127/nsurlsession-tutorial 查理·富尔顿 2013年10月9日, 推特 注意从雷 :这是一个缩写版的一章 i ...
- SqlServer日期查询
一.sql server日期时间函数 Sql Server中的日期与时间函数 1. 当前系统日期.时间 select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返 ...
- DOM 对象之 document.all
1.document.all是页面内所有元素的一个集合: 2.经测试在chrome,safari,opera,ie中均返回一个HTMLALLCollection[xx]对象,在FF中返回是一个unde ...