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 ...
随机推荐
- cf448A Rewards
A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- 清除IE输入框眼睛和叉叉
/* 清除IE输入框眼睛和叉叉 */::-ms-clear { display: none; } ::-ms-reveal { display: none; }
- Word Break II 解答
Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...
- apacheOfbiz
一.ofbiz 用自身数据库安装 1. 由 binary 安装: 由 binary 安装非常简单, 以下是安装方法: 下载ofbiz-2.0-beta1-complete.tar.gz, 注意不是of ...
- fcntl,F_GETFL,F_SETFL,flags
1.获取文件的flags,即open函数的第二个参数: flags = fcntl(fd,F_GETFL,0); 2.设置文件的flags: fcntl(fd,F_SETFL,flags); 3.增加 ...
- ListView之BaseAdapter
BaseAdapter可以实现自定义的丰富子项视图,本文实现如下所示结果: 实现代码: /* ListView :列表 BaseAdapter 通用的基础适配器 * * */ public class ...
- mbed OS - ARM关于物联网(IoT)的战略布局
关于IoT 在刚刚过去的ARMTECHCON2014(Santa Clara Convention Center)第1天会议,首要的keynote就是ARM针对建立物联网(InternetOf Thi ...
- 同一个页面多个CALayer重绘的办法
//知识点,CALayer的重绘,-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 方法,CALayer的渐变色.多个CALa ...
- Qt使用异或进行加密解密
在加密,解密中,异或运算应该时比较简单的一种.下面的代码,采用异或运算进行加密,解密: 点击(此处)折叠或打开 #include <QtCore/QCoreApplication&g ...
- swift——设置navigationitemtitle的内容以及格颜色
1.用UILabel,自定义整个titleview // var TitleText = UILabel() self.TitleText.frame = CGRectMake(0, 0, 100, ...