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 解答的更多相关文章

  1. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  2. Palindrome Permutation II 解答

    Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

  3. Word Pattern II 解答

    Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  4. [Leetcode] n queens ii n皇后问题

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  5. Ugly Number II 解答

    Question Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime ...

  6. 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 ...

  7. Populating Next Right Pointers in Each Node II 解答

    Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...

  8. Word Break II 解答

    Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...

  9. 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 ...

  10. Majority Element II 解答

    Question Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Th ...

随机推荐

  1. 《Two Days DIV + CSS》读书笔记——CSS选择器

    1.1.2 CSS选择器 CSS 选择器最基本的有四种:标签选择器.ID 选择器.类选择器.通用选择器. [标签选择器] 一个完整的 HTML 页面由很多不同的标签组成,而标签选择器,则是决定哪些标签 ...

  2. poj 3616 Milking Time(dp)

    Description Bessie ≤ N ≤ ,,) hours (conveniently labeled ..N-) so that she produces as much milk as ...

  3. StaggeredGridView+universal-image-loader载入网路图片实现瀑布流

    StaggeredGridView 开源lib  https://github.com/maurycyw/StaggeredGridView 文章demo下载地址  http://download.c ...

  4. JS数组追加数组采用push.apply的坑(转)

    JS数组追加数组没有现成的函数,这么多年我已经习惯了a.push.apply(a, b);这种自以为很酷的,不需要写for循环的写法,一直也没遇到什么问题,直到今天我要append的b是个很大的数组时 ...

  5. Android --------- 标签include位置设置无效

    给include设置below或align无效,是因为没有给include设置width和height.

  6. .NET winform 的keypress事件中判断当用户按下的是哪个键

    keys是按键的枚举类型 private void txtPropertyValue_KeyPress(object sender, KeyPressEventArgs e) { if ((Keys) ...

  7. C#.NET 各种连接字符串

    C#.NET 各种连接字符串 近期连接数据库时,经常忘记连接字符串是如何的格式,现在此备注 此文章引用http://www.cnblogs.com/zhiqiang-imagine/archive/2 ...

  8. 认识WebSocket理论篇

    本文转自http://www.ibm.com/developerworks/cn/java/j-lo-WebSocket/ HTML5作为下一代WEB标准,拥有许多引人注目的新特性,如Canvas.本 ...

  9. CAN总线

    1.CAN(controller area network) LAN(local area network)局域网 2.CAN等通信协议的开发,使多种LAN通过网关进行数据交换得以实现.如应用在汽车电 ...

  10. (转)Java程序利用main函数中args参数实现参数的传递

    Java程序利用main函数中args参数实现参数的传递 1.运行Java程序的同时,可以通过输入参数给main函数中的接收参数数组args[],供程序内部使用!即当你在Java命令行后面带上参数,J ...