皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

上图为 8 皇后问题的一种解法。

给定一个整数 n,返回 n 皇后不同的解决方案的数量。

示例:

输入: 4
输出: 2
解释: 4 皇后问题存在如下两个不同的解法。
[
 [".Q..",  // 解法 1
  "...Q",
  "Q...",
  "..Q."],  ["..Q.",  // 解法 2
  "Q...",
  "...Q",
  ".Q.."]
]
只需要把51题的return条件换掉就行,注意用static时,服务器进行测试1的时候很容易出错。故变为数组进行存储结果。。。不求上进
 class Solution {
public static int totalNQueens(int n) {
if(n <=0)return 0;
int[] res={0};
helper(new int[n],0,res);
return res[0];
}
public static void helper(int[] queens,int pos,int[] res){
if(pos == queens.length){
res[0] += 1;
return;
}
for(int i = 0 ;i < queens.length;i++){
queens[pos] = i;
if(isValid(queens,pos)){
helper(queens,pos+1,res);
}
}
}
public static boolean isValid(int[] queens,int pos){
for(int i = 0;i < pos;i++){
if(queens[i] == queens[pos])return false;
else if(Math.abs(queens[i] - queens[pos]) == Math.abs(i - pos)){
return false;
}
}
return true;
} }

2019-05-10 16:19:39

LeetCode--052--N皇后II(java)的更多相关文章

  1. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...

  2. leetcode 126. Word Ladder II ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  3. leetcode 40 Combination Sum II --- java

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. [LeetCode] 52. N皇后 II

    题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...

  5. 【LeetCode 】N皇后II

    [问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...

  6. [leetcode] 45. 跳跃游戏 II(Java)(动态规划)

    45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...

  7. leetcode 140. Word Break II ----- java

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. leetcode 137. Single Number II ----- java

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  9. leetcode 132. Palindrome Partitioning II ----- java

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

随机推荐

  1. MK66FN2M0VLQ18

    NXP Kinetis K66: 180MHz Cortex-M4F MCU, 2MB Flash, 256KB SRAM, Dual USBs (FS + HS), Ethernet, 144-LQ ...

  2. SpringBoot系列:一、SpringBoot搭建

    打开IDEA,新建一个spring工程,然后无脑下一步就行. 新建完成后的目录结构 java文件夹下是java源码 resources下是配置文件 test下是测试文件 添加web模块支持,在pom. ...

  3. 修改属性item1(1变化)

    给imgList1,7,12,16添加数据 数据层data:{imgList1:[],imgList7:[],imgList12:[],imgList16:[],} 处理层let _this=this ...

  4. Angular5 import interface 报错:XXX is not a module

    在项目里定义了一个interface,device.ts.然后在component.ts中要使用这个interface,import之后,VSCode报错:‘xxx/xxx/xxx/device.ts ...

  5. docker--虚拟化

    1 什么是虚拟化 1.1 概念 在计算机中,虚拟化(英语:Virtualization)是一种资源管理技术,是将计算机的各种 实体资源,如服务器.网络.内存及存储等,予以抽象.转换后呈现出来,打破实体 ...

  6. Implement Queue using Stacks(用两个栈实现队列)

    来源:https://leetcode.com/problems/implement-queue-using-stacks Implement the following operations of ...

  7. python之getopt

    getopt可以分析输入的参数,根据不同的参数输入不同的命令 getopt.getopt( [命令行参数列表], "短选项", "长选项列表" ) getopt ...

  8. 【mysql连接与mysqldump】INFORMATION_SESSION_VARIABLES feature is disabled

    摘要: mysql5.7 客户端连接出现:INFORMATION_SESSION_VARIABLES feature is disabled问题: mysqldump: Couldn't execut ...

  9. HDFS数据流——读数据流程

    HDFS读数据流程 假设客户端请求下载文件/user/atguigu/ss.avi,HDFS读数据流程如下: 1)客户端向namenode请求下载文件,namenode通过查询元数据,找到文件所有文件 ...

  10. python 定义模块作用及分类

    python把一个功能的模块归类,简单来说,模块是一个由Python代码组成的文件.模块可以定义函数,类和变量. 模块还可以包括可运行的代码. 1,python模块的作用 提高代码的方便维护 使用模块 ...