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

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

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

示例:

  1. 输入: 4
  2. 输出: 2
  3. 解释: 4 皇后问题存在如下两个不同的解法。
  4. [
  5.  [".Q..",  // 解法 1
  6.   "...Q",
  7.   "Q...",
  8.   "..Q."],
  9.  
  10.  ["..Q.",  // 解法 2
  11.   "Q...",
  12.   "...Q",
  13.   ".Q.."]
  14. ]
    只需要把51题的return条件换掉就行,注意用static时,服务器进行测试1的时候很容易出错。故变为数组进行存储结果。。。不求上进
  1. class Solution {
  2. public static int totalNQueens(int n) {
  3. if(n <=0)return 0;
  4. int[] res={0};
  5. helper(new int[n],0,res);
  6. return res[0];
  7. }
  8. public static void helper(int[] queens,int pos,int[] res){
  9. if(pos == queens.length){
  10. res[0] += 1;
  11. return;
  12. }
  13. for(int i = 0 ;i < queens.length;i++){
  14. queens[pos] = i;
  15. if(isValid(queens,pos)){
  16. helper(queens,pos+1,res);
  17. }
  18. }
  19. }
  20. public static boolean isValid(int[] queens,int pos){
  21. for(int i = 0;i < pos;i++){
  22. if(queens[i] == queens[pos])return false;
  23. else if(Math.abs(queens[i] - queens[pos]) == Math.abs(i - pos)){
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29.  
  30. }

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. ConcurrentLinkedQueue 源码分析

    ConcurrentLinkedQueue ConcurrentLinkedQueue 能解决什么问题?什么时候使用 ConcurrentLinkedQueue? 1)ConcurrentLinked ...

  2. git 还原、恢复、回退

    通过git revert来实现线主干代码的回滚.如下命令 对于 merge类型的commit对象,还需要"-m"参数 git revert -m 1  commit-id 对于普通 ...

  3. 005-spring-data-elasticsearch 3.0.0.0使用【三】-spring-data之Spring数据扩展

    续 1.8.Spring数据扩展 这些扩展使Spring Data在各种环境下的使用成为可能.目前大部分的整合都是针对Spring MVC. 1.8.1.Querydsl扩展 Querydsl是一个框 ...

  4. astype()函数

    1astype()函数可用于转化dateframe某一列的数据类型 如下将dateframe某列的str类型转为int,注意astype()没有replace=True的用法,想要在原数据上修改,要写 ...

  5. 佳能mp288拆解步骤--绝对原创

    http://itbbs.pconline.com.cn/office/50663206.html 佳能mp288拆解步骤--绝对原创 gotobug Lv1太平洋舰队新兵 楼主 2013-10-13 ...

  6. python每日一练:0001题

    第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? import o ...

  7. 003/node.js--代理服务(解决跨域问题)

    业务描述: 1.web前端发送http请求 2.web后端为https协议 如何保证web前端发送http请求到web后端(跨域问题:域名不一致即跨域), 因此用node.js写了个代理服务,转发前端 ...

  8. springSecurity5 重定向登录页面后 报错:尝试清除 Cookie.net::ERR_TOO_MANY_REDIRECTS status:200

    springSecurity5 使用: http.formLogin().loginPage("/login");报错如下图: springsucurity5 中 需要给 自己定义 ...

  9. 前端 CSS层叠性 CSS选择器优先级

    层叠性 层叠性:权重的标签覆盖掉了权重小的标签,说白了 ,就是被干掉了 权重:谁的权重大,浏览器就会显示谁的属性 我们现在已经学过了很多的选择器,也就是说在一个HTML页面中有很多种方式找到一个元素并 ...

  10. python函数-基础知识

    一.含义函数是程序内的“小程序” 二.示例 #!/usr/bin/env python #coding:utf-8 def hello(): print('Hello world!') print(' ...