题目:

Follow up for N-Queens problem.

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

题意:

接上题N-Queens problem,这里仅仅是要求返回全部的不同的解的数目。

算法分析:

思路与上题同样,代码比上题更加简洁了

AC代码:

<span style="font-size:12px;">public class Solution
{
private int res=0;
public int totalNQueens(int n)
{
int[] loc = new int[n]; //记录皇后处于哪一列,列数组
dfs(loc,0,n);
return res;
}
public void dfs(int[] loc, int cur, int n)
{
if(cur==n)
res++;
else
{
for(int i=0;i<n;i++)
{
loc[cur] = i;
if(isValid(loc,cur))
dfs(loc,cur+1,n); //再放皇后m+1, 假设皇后m+1放完并返回了
//两种可能: 1:冲突,返回了 2.一直将所有的皇后所有放完并安全返回了
//将皇后m回溯。探索新的可能或者安全的位置 --->
}
}
}
public boolean isValid(int[] loc, int cur)
{
for(int i=0;i<cur;i++)//仅仅须要保证与那些已经就位的皇后不冲突就可以
{
if(loc[i]==loc[cur]||Math.abs(loc[i]-loc[cur])==(cur-i)) //验证对角线,依据对角线性质,长 = 宽
//那么我们不难写出 Math.abs(loc[i] - loc[cur]) == (cur - i)
return false;
}
return true;
} }</span>

[LeetCode][Java] N-Queens II的更多相关文章

  1. [Leetcode][JAVA] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  2. [Leetcode][JAVA] Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [LeetCode][Java] Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  4. [LeetCode][Java] Jump Game II

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  5. N-Queens II leetcode java

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

  6. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  7. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  8. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  9. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  10. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

随机推荐

  1. wordpress网站底部的运行时间是怎么设置的?

    别人网站底部显示的网站运行时间是什么设置的?是插件的效果吗? 不用插件,一段JS代码就可以实现同样的效果. 复制如下代码到 footer.php 页脚那里 <span id="runt ...

  2. es6数值扩展

    1. 二进制和八进制表示法 从 ES5 开始,在严格模式之中,八进制就不再允许使用前缀0表示,ES6 进一步明确,要使用前缀0o表示. ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0 ...

  3. NodeJS、NPM安装配置步骤

    安装NodeJS和NPM 1.Node JS 官网下载地址 https://nodejs.org/en/download/ 2.安装完后,使用cmd 命令输入两个命令,查看安装状态 node -v n ...

  4. HFS模板开发

    痉挛模板, 节, 符号 & 变量帮助需要更多帮助 吗?看看 下面这些链接-模板是模型痉挛用于构建 HTML 页面. -它分为几个部分, 每个部分描述最终 HTML 页面的一部分. -此模板必须 ...

  5. gearman的安装与使用

    Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务.php官方收录:http://php.net/manual/zh ...

  6. css--小白入门篇4

    一.前文回顾 盒模型box model 什么是盒子?所有的标签都是盒子.无论是div.span.a都是盒子.图片.表单元素一律看做文本. 盒模型有哪些组成:width.height.padding.b ...

  7. C++ 实现Golang里的defer

    不多说了,直接贴代码.就一个hpp文件. 1 #include <functional> 2 3 #define CONCAT_(a, b) a##b 4 #define CONCAT(a ...

  8. 关于C++中字符串输入get与getline的区别

    最近使用C++中自己老是忘记的一个点,get与getline的区别. 1.get与getline get和getline所属iostream类,作用是读取一整行,通过换行符确定读取结束,他们都可以读取 ...

  9. Linux学习笔记记录(二)

  10. linux free命令-显示内存的使用情况

    更多Linux 性能监测与优化 关注 Linux命令大全 free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. 语法 free(选项) 选项 -b:以Byte为单 ...