题目:

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. micropython陀螺仪控制舵机

    2018-03-1220:14:00 import pyb import time from pyb import Pin xlights = (pyb.LED(2),pyb.LED(3)) MO = ...

  2. public private protected

    初学C++的朋友经常在类中看到public,protected,private以及它们在继承中表示的一些访问范围,很容易搞糊涂.今天本文就来十分分析一下C++中public.protected及pri ...

  3. 浅谈FFC

    FFC(Flexible Formatting Context) CSS3引入了一种新的布局模型——flex布局(之前有文章介绍过).flex是flexible box的缩写,一般称之为弹性盒模型.和 ...

  4. 05JavaScript中的事件处理

    JavaScript中的事件处理 在JavaScript中,事件的发生主要是由窗口中内容变化.键盘和鼠标引起的.JavaScript在某些事件发生的时候,可以通过一些相应的事件处理器来捕获这些事件,并 ...

  5. JAVA基础数组

    数组: 数组是一种容器 存储同一类型的一组数据(必须是 类型相同的一组数据) 定义数组的公式:(有两种) 1.静态定义      1)数据类型[ ] 数组名 = {元素1,元素2,元素3,元素4,元素 ...

  6. PHP 结合前端 ajax 爬取网站信息后, 向指定用户发送指定短信;

    <?php /** * Description * @authors Your Name (you@example.org) * # 根据时时彩的最新一期的号码, 判断如果为首尾同号则发送短信 ...

  7. INT32 System_UserKeyFilter(NVTEVT evt, UINT32 paramNum, UINT32 *paramArray)

    INT32 System_UserKeyFilter(NVTEVT evt, UINT32 paramNum, UINT32 *paramArray){    UINT32 key = evt; if ...

  8. 洛谷 1097 统计数字(NOIp2007提高组T1)

    [题解] 排个序然后扫一遍进行统计即可. #include<cstdio> #include<algorithm> #include<cstring> #defin ...

  9. MongoDB增加用户、删除用户、修改用户读写权限及只读权限(注:转载于http://www.2cto.com/database/201203/125025.html)

    MongoDB  增加用户 删除用户  修改用户  读写权限 只读权限,   MongoDB用户权限分配的操作是针对某个库来说的.--这句话很重要.   1. 进入ljc 数据库:       use ...

  10. HDU 1540 区间合并线段树

    题目大意: 就是给定一堆位置,进行删除还原,最后找到 t 位置上的最大连续位置 #include <cstdio> #include <cstring> #include &l ...