N-Queens II 解答

时间:2021-10-31 20:50:39

Question

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

N-Queens II 解答

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. &lbrack;Leetcode&rsqb; 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. &period;Net4&period;0的网站在IE10、IE11出现&OpenCurlyDoubleQuote;&lowbar;&lowbar;doPostBack未定义”的解决办法。

    方法一.浏览器设置成兼容模式. 方法二.安装服务器版的.Net40的补丁.http://download.csdn.net/detail/5653325/6642051 方法三.点击VS的工具菜单-- ...

  2. WordPress Checkout插件跨站脚本漏洞和任意文件上传漏洞

    漏洞名称: WordPress Checkout插件跨站脚本漏洞和任意文件上传漏洞 CNNVD编号: CNNVD-201311-015 发布时间: 2013-11-04 更新时间: 2013-11-0 ...

  3. A Statistical View of Deep Learning &lpar;V&rpar;&colon; Generalisation and Regularisation

    A Statistical View of Deep Learning (V): Generalisation and Regularisation We now routinely build co ...

  4. CodeForces 707D Persistent Bookcase

    $dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...

  5. 深入剖析MSAA

    本文打算对MSAA(Multisample anti aliasing)做一个深入的讲解,包括基本的原理.以及不同平台上的实现对比(主要是PC与Mobile).为了对MSAA有个更好的理解,所以写下了 ...

  6. python3学习笔记九(if语句)

    # !/usr/bin/python3 斐波那数列,两个元素的总和确定下一个数a,b = 0,1while b < 1000: print(b,end=',') a, b = b, a+bpri ...

  7. java基础题刷题中的知识点复习

    将变量转换为字符串方法:(String)待转对象..toString().String.valueOf(待转对象) 对字符串进行操作的方法,使用StringBuffer和StringBuilder定义 ...

  8. LomBok插件的使用

    LomBok插件的使用 By Zhai 简介: LomBok是一个通过简单注解就可以减少一些冗余代码编写的小工具.例如 @Setter @Getter 用于实例类上该类就不需要写set get 方法. ...

  9. android 开发 框架系列 使用 FileDownloader 实现检查更新的功能class

    首先介绍一下FileDownloader GH :https://github.com/lingochamp/FileDownloader/blob/master/README-zh.md FileD ...

  10. 【bzoj3224】 Tyvj1728—普通平衡树

    http://www.lydsy.com/JudgeOnline/problem.php?id=3224 (题目链接) 题意 1. 插入x数:2. 删除x数(若有多个相同的数,因只删除一个):3. 查 ...