LeetCode--051--N皇后(java)-star
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
上图为 8 皇后问题的一种解法。
给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。
每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q'
和 '.'
分别代表了皇后和空位。
示例:
输入: 4
输出: [
[".Q..", // 解法 1
"...Q",
"Q...",
"..Q."], ["..Q.", // 解法 2
"Q...",
"...Q",
".Q.."]
]
解释: 4 皇后问题存在两个不同的解法。 思路:
按上述解法1queens数组为[1,3,0,2] 因为没以行就一个queens所以不用考虑行了,只纪录该行哪一列放了Q
先建立queens数组(建立好后用addSolution填好Q和point),用回溯的方法,把所有满足题意的方式都穷举出来.
TIME:O(N^N)?
SPACE:O(N)
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<>();
if(n <= 0)return res; helper(res,new int[n],0);
return res;
}
public void helper(List<List<String>> res,int[] queens,int pos){
if(pos == queens.length){
addSolution(res,queens);
return;
} for(int i = 0;i < queens.length;i++){
queens[pos] = i;
if(isValid(queens,pos)){
helper(res,queens,pos+1);
}
}
}
public 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[pos] - queens[i]) == Math.abs(i - pos)){//在同一对角线上
return false;
}
}
return true;
}
public void addSolution(List<List<String>> res ,int[] queens){
List<String> list = new ArrayList<>();
for(int i = 0;i < queens.length;i++){
StringBuilder sb = new StringBuilder();
for(int j = 0;j < queens.length;j++){
if(queens[i] == j){
sb.append('Q');
}else{
sb.append('.');
}
}
list.add(sb.toString()); }
res.add(list);
}
}
2019-05-08 20:51:23
python版本,秒杀java
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
def DFS(queens,xy_dif,xy_sum):
p = len(queens)
if p == n:
result.append(queens)
return None
for q in range(n):
if q not in queens and p-q not in xy_dif and p+q not in xy_sum:
DFS(queens+[q],xy_dif+[p-q],xy_sum+[p+q])
result = []
DFS([],[],[])
return [["."*i + "Q" + "."*(n-i-1) for i in sol]for sol in result]
2020-01-13 16:38:14
LeetCode--051--N皇后(java)-star的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Java for LeetCode 051 N-Queens
Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a d ...
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- Java实现 LeetCode 51 N皇后
51. N皇后 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决 ...
- [LeetCode] N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- 八皇后java算法
import java.util.Date; public class EightQueen { public static void main(String[] args) { long star ...
- 极限编程,最强N皇后JAVA解题代码,4秒出15皇后,33秒出16皇后
私人博客原文链接来自:http://www.hexcode.cn/article/show/eight-queen 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化 8皇后以 ...
随机推荐
- bash如何向上向下移动历史命令记录
bash如何在 历史命令记录 中上下移动? 通过bash本身的设置,就可以做到! : 用shift+up , shift+down 来上下移动 : 单纯的up/down是调用历史命令
- python异常处理(try-except)
什么是异常? 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行. 一般情况下,在Python无法正常处理程序时就会发生一个异常. 异常是Python对象,表示一个错误. 当Pyth ...
- beanstalkd 安装和配置
安装 安装以centos为例 yum install beanstalkd 配置 使用centos yum安装,通过查看服务脚本发现有这个配置文件 cat /etc/sysconfig/beansta ...
- Altium Designer chapter8总结
元件库操作中需要注意如下: (1)原理图库:主要分三部分来完成(绘制元件的符号轮廓.放置元件引脚.设计规则检查). (2)多子件原理图库:操作与原理图库基本相同,就是新建part. (3)PCB封装库 ...
- 【HANA系列】SAP HANA计算视图(calculation views)使用RANK报错
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA计算视图(cal ...
- 剑指offer--day01
1.1题目:二维数组中的查找:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断 ...
- openstack 制作镜像以及windows向Linux中通过xshell传文件
慢慢的也要把openstack一些相关的笔记整理上来了 之前由于主要是在看horizon 实验室搭建的openstack平台并没有怎么实际的用起来,前几天别的同学要用来测试大数据的相关服务,才把这些内 ...
- springSecurity5 重定向登录页面后 报错:尝试清除 Cookie.net::ERR_TOO_MANY_REDIRECTS status:200
springSecurity5 使用: http.formLogin().loginPage("/login");报错如下图: springsucurity5 中 需要给 自己定义 ...
- 优化内存_内存泄漏——C
内存泄漏: 动态申请内存,没有正常释放,后续又申请内存,也没释放内存,导致内存池被全部被占用,最终再申请内存的时候失败:严格点则每次申请内存的时候判断申请到的指针是否为空,若为空NULL则表示申请失 ...
- 使用userAgent区分浏览器版本
使用userAgent区分浏览器版本:https://blog.csdn.net/n447194252/article/details/76255489 JS根据userAgent值来判断浏览器的类型 ...