LeetCode--052--N皇后II(java)
n 皇后问题研究的是如何将 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)的更多相关文章
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- leetcode 40 Combination Sum II --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 52. N皇后 II
题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...
- 【LeetCode 】N皇后II
[问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- 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 ...
- leetcode 137. Single Number II ----- java
Given an array of integers, every element appears three times except for one. Find that single one. ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- ConcurrentLinkedQueue 源码分析
ConcurrentLinkedQueue ConcurrentLinkedQueue 能解决什么问题?什么时候使用 ConcurrentLinkedQueue? 1)ConcurrentLinked ...
- git 还原、恢复、回退
通过git revert来实现线主干代码的回滚.如下命令 对于 merge类型的commit对象,还需要"-m"参数 git revert -m 1 commit-id 对于普通 ...
- 005-spring-data-elasticsearch 3.0.0.0使用【三】-spring-data之Spring数据扩展
续 1.8.Spring数据扩展 这些扩展使Spring Data在各种环境下的使用成为可能.目前大部分的整合都是针对Spring MVC. 1.8.1.Querydsl扩展 Querydsl是一个框 ...
- astype()函数
1astype()函数可用于转化dateframe某一列的数据类型 如下将dateframe某列的str类型转为int,注意astype()没有replace=True的用法,想要在原数据上修改,要写 ...
- 佳能mp288拆解步骤--绝对原创
http://itbbs.pconline.com.cn/office/50663206.html 佳能mp288拆解步骤--绝对原创 gotobug Lv1太平洋舰队新兵 楼主 2013-10-13 ...
- python每日一练:0001题
第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? import o ...
- 003/node.js--代理服务(解决跨域问题)
业务描述: 1.web前端发送http请求 2.web后端为https协议 如何保证web前端发送http请求到web后端(跨域问题:域名不一致即跨域), 因此用node.js写了个代理服务,转发前端 ...
- springSecurity5 重定向登录页面后 报错:尝试清除 Cookie.net::ERR_TOO_MANY_REDIRECTS status:200
springSecurity5 使用: http.formLogin().loginPage("/login");报错如下图: springsucurity5 中 需要给 自己定义 ...
- 前端 CSS层叠性 CSS选择器优先级
层叠性 层叠性:权重的标签覆盖掉了权重小的标签,说白了 ,就是被干掉了 权重:谁的权重大,浏览器就会显示谁的属性 我们现在已经学过了很多的选择器,也就是说在一个HTML页面中有很多种方式找到一个元素并 ...
- python函数-基础知识
一.含义函数是程序内的“小程序” 二.示例 #!/usr/bin/env python #coding:utf-8 def hello(): print('Hello world!') print(' ...