题目:

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. 通过机智云APP来学习安卓

    效果非常之好,安卓6.0之后就进行了动态授权.按照网上的视频一步一步调试的非常成功,非常舒服.

  2. Codewars练习Python

    计算一个数组的中间数,数的两边和相等,并返回index值 如:数组[1,2,3,4,6] 返回3(数组序号从0开始) def find_even_index(arr): ""&qu ...

  3. spring加载classpath与classpath*的区别别

    1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件. 2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文 ...

  4. java如何区分同时继承的父类和实现的接口中相同的方法

    基类代码: public class Father { public Father() { System.out.println("基类构造函数{"); show(); Syste ...

  5. NGS数据格式介绍

    一般情况下,从Illumina平台上得到的测序,其数据格式是Fastq格式,可以称之为原始数据(Raw data).事实上直接的下机数据是显微拍摄得到的图像信息.但是一般都会用Bcl2Fastq软件将 ...

  6. JAVA学习笔记16——线程的创建和启动

    Java使用Thread类代表线程,所有的线程对象都必须是Thread类或其子类的实例.每个线程的作用是完成一定的任务,实际上就是执行一段程序流(一段顺序执行的代码).Java使用线程执行体来代表这段 ...

  7. 比较synchronized和读写锁

    一.科普定义 这篇博文的两个主角“synchronized”和“读写锁” 1)synchronized 这个同步关键字相信大家都用得比较多,在上一篇“多个线程之间共享数据的方式”中也详细列举他的应用, ...

  8. block的作用

    ios高效开发--blocks相关   1.替换delegate       如果我们有2个viewController,a和b,当我们从a界面push到b后,在b上面触发了一些事件,这些时间又会影响 ...

  9. 字符串--P1553 数字反转(升级版)

    题目描述 给定一个数,请将该数各个位上数字反转得到一个新数. 这次与NOIp2011普及组第一题不同的是:这个数可以是小数,分数,百分数,整数.整数反转是将所有数位对调:小数反转是把整数部分的数反转, ...

  10. anaconda镜像

    下载一个包老是下载不下来,于是放弃了官方版改为国内镜像. 清华镜像 (打开Anaconda Prompt   或者 打开cmd,运行下面的命令) conda config --add channels ...