[LeetCode][Java] N-Queens II
题目:
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的更多相关文章
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [Leetcode][JAVA] Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode][Java] Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- [LeetCode][Java] Jump Game II
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- Ubantu下 docker的安装
另一种方法 1.切换到root权限或者用sudo 2.升级source列表并保证https和ca证书成功安装 # apt-get update # apt-get install apt-transp ...
- [Windows Server 2008] IP安全策略限制端口方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:限制143 ...
- python学习小结-字典key,val互换
第一种,使用压缩器: >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> zip(m.values(), m.keys()) ...
- StyleAI厚积薄发: Android网络图片数据传输
在StyleAI上厚积了这么长时间,憋了这么久,本来想憋个更大的,不过还是薄发一次的好. 三.直接使用别人的工程 文章:Android学习之客户端上传图片到服务器 下载地址:https://downl ...
- java 动态代理 和动态编程
概述 代理分两种技术,一种是jdk代理(机制就是反射,只对接口操作),一种就是字节码操作技术.前者不能算技术,后者算是新的技术.未来将有大的动作或者较为广泛的应用和变革,它可以实现代码自我的编码(人工 ...
- 梦想CAD控件图层COM接口知识点
梦想CAD控件图层COM接口知识点 一.新建图层 主要用到函数说明: _DMxDrawX::AddLayer 增加新的图层.详细说明如下: 参数 说明 BSTR pszName 图层名 c#中实现代码 ...
- oracle_backup
#!/bin/bash DAYS=`date +"%Y%m%d"` . /home/oracle/.bash_profile # /home/opt/oracle/11g/bin/ ...
- react native 从头开始
1.react-native run-android 报错SDK location not found. Define location with sdk.dir in the local.prope ...
- linux ifstat-统计网络接口流量状态
推荐:更多linux 性能监测与优化 关注:linux命令大全 ifstat命令就像iostat/vmstat描述其它的系统状况一样,是一个统计网络接口活动状态的工具.ifstat工具系统中并不默认安 ...
- python3 时间模块 random模块之两个小练习
话不多说,一个是算时间的,还有一个是生成验证码的 #!usr/bin/env/ python # -*- coding:utf-8 -*- # Author: XiaoFeng import time ...