Follow up for N-Queens problem.

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

计算N皇后的合法解的数量。

解题思路:这次使用了更优化的方式判断棋盘上的冲突,abs(curr.row-row)==abs(curr.col-col)

board数组纪录每行的列的位置,如board[1]=3,代表第一行第三列上有皇后。

public class Solution {

    int[] board;
int res = 0;
public int totalNQueens(int n) {
board = new int[n];
helper(0,n);
return res;
} private void helper(int k,int n){
if(k>=n){
res++;
return;
}
for(int i=0;i<n;i++){
if(valid(k,i)){
board[k]=i;
helper(k+1,n);
}
}
return ;
} private boolean valid(int x, int y){
int row = 0;
while(row<x){
if(board[row]==y||Math.abs(x-row)==Math.abs(y-board[row])){
return false;
}
row++;
}
return true;
}
}

N-Queens II——Leetcode的更多相关文章

  1. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

  2. Subsets II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...

  3. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

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

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

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

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

  6. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  7. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  8. amazon oa1 - search in 2D array II [Leetcode] 240

    https://leetcode.com/problems/search-a-2d-matrix-ii/ 巧解题,矩阵本身等于了一个binary search tree,从中值开始走 时间复杂度 O( ...

  9. Combination Sum II [LeetCode]

    Problem description: http://oj.leetcode.com/problems/combination-sum-ii/ Basic idea: use recursive a ...

  10. Best Time to Buy and Sell Stock II [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Basic idea: ...

随机推荐

  1. CSS样式权值

    内联样式表(InLine style)>内部样式表(Internal style sheet)>外部样式表(External style sheet) 例外:但如果外部样式表放在内部样式表 ...

  2. sublime的js调试环境(基于node环境)

    很多的语言都有控制台,都要专门的ide,js,用sublime在node的环境下,可以配置console, 如何配置?首先,要有node的环境,下载安装,还要安装到c盘才行,然后找到'工具(tool) ...

  3. Getting Started with Testing ——开始单元测试

    Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as i ...

  4. 某PHP代码加密

    <?php /* 本程序已加密: 2014-11-15 10:10:11 */ xs_run('JGxosS9QplmqLA6qjYo/LiX5ecUe0DH7p42Ww/Mdkf5/ybZDs ...

  5. asp.net 批量下载实现(打包压缩下载)

    1.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default ...

  6. angular的post提交

    用下来明显感觉jquery的post提交比ng的post提交好用很多 一开始,用angularjs的$http提交的数据,在php服务器端无法通过 因为jQuery会把作为JSON对象的data序列化 ...

  7. AbstractFactory 模式

    ///////////////////////Product.h////////////// #ifndef _PRODUCT_H_ #define _PRODUCT_H_ class Abstrac ...

  8. SGU 115.Calendar

    连水3道,还能更水么... #include <stdio.h> using namespace std; ] = {, , , , , , , , , , , , }; int n, m ...

  9. printf 格式化最常用用法

    printf 操作符的参数包括”格式字符串“及”要输出的数据列表". 格式字符串好像用来填空的模版,代表你想要的输出格式: printf "Hello,%s;your passwo ...

  10. Javascript参数传递中值和引用的一种理解

    值(value)和引用(reference)是各种编程语言老生常谈的话题,js也不例外. 我将剖析一个例子的实际运行过程,跟大家分享我对js参数传递中的值和引用的理解. 参考官网数据类型的两种分类,本 ...