Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

Example:

Input: 4
Output: [
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."], ["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<>();
List<String> list = new ArrayList<>();
if (n == 0) {
return res;
}
helper(res, list, 0, n);
return res;
} private void helper(List<List<String>> res, List<String> list, int level, int n) {
if (level == n) {
res.add(new ArrayList<>(list));
return;
}
char[] charArr = new char[n];
Arrays.fill(charArr, '.');
for (int i = 0; i < n; i++) {
charArr[i] = 'Q';
if (isValid(i, list)) {
list.add(new String(charArr));
helper(res, list, level + 1, n);
list.remove(list.size() - 1);
}
charArr[i] = '.';
}
} private boolean isValid(int column, List<String> list) {
int row = list.size();
for (int i = 0; i < row; i++) {
String cur = list.get(i);
int col = cur.indexOf("Q");
if (col == column || row - i == Math.abs(col - column)) {
return false;
}
}
return true;
}
}

[LC] 51. N-Queens的更多相关文章

  1. Spring Boot文档

    本文来自于springboot官方文档 地址:https://docs.spring.io/spring-boot/docs/current/reference/html/ Spring Boot参考 ...

  2. [Leetcode][Python]51: N-Queens

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 51: N-Queenshttps://oj.leetcode.com/pro ...

  3. CentOS7进行OpenStack(queens)最小化部署实验出现的问题与解决过程

    注:此文为<OpenStack(queens)最小化搭建记录——控制与计算共两个节点>的补充 1.chrony时间同步服务搭建的时候,出现计算节点无法与控制节点同步.(controller ...

  4. 5-1可视化库Seabon-整体布局风格设置

    In [1]: import seaborn as sns import numpy as np import matplotlib as mpl import matplotlib.pyplot a ...

  5. [LeetCode] 51. N-Queens N皇后问题

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  6. LC T668笔记 & 有关二分查找、第K小数、BFPRT算法

    LC T668笔记 [涉及知识:二分查找.第K小数.BFPRT算法] [以下内容仅为本人在做题学习中的所感所想,本人水平有限目前尚处学习阶段,如有错误及不妥之处还请各位大佬指正,请谅解,谢谢!] !! ...

  7. 记一次jdk升级引起的 Unsupported major.minor version 51.0

    之前jdk 一直是1.6,tomcat 是6.x 版本,, 现在引入的新的jar, 出现 Caused by: java.lang.UnsupportedClassVersionError: org/ ...

  8. 编写高质量代码:改善Java程序的151个建议(第3章:类、对象及方法___建议47~51)

    建议47:在equals中使用getClass进行类型判断 本节我们继续讨论覆写equals的问题,这次我们编写一个员工Employee类继承Person类,这很正常,员工也是人嘛,而且在JavaBe ...

  9. 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。

    laviewpbt  2014.8.4 编辑 Email:laviewpbt@sina.com   QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...

随机推荐

  1. 第二阶段scrum-4

    1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 前端制作完成,数据库在制作中.

  2. Docker 容器(container)

    版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...

  3. Java中的Math.abs()

    Math.abs(n):对int.long.float.double类型的数取绝对值 其中 int 类型的数取值范围是 -2^31——2^31-1(-2147483648 ~ 2147483647) ...

  4. X2安装配置keras环境(包含matplotlib安装)

    https://blog.csdn.net/jonado13/article/details/83933453 1.安装pipapt install python3-pipE: Could not o ...

  5. 19 01 16 djano 视图以及url

    视图 后台管理页面做好了,接下来就要做公共访问的页面了.当我们刚刚在浏览器中输入 http://127.0.0.1:8000/admin/ 之后,浏览器显示出了后台管理的登录页面,那有没有同学想过这个 ...

  6. zabbix_server

    http://www.linuxidc.com/Linux/2014-11/109909.htm [root@localhost zabbix]# service iptables stop  关闭i ...

  7. 大二暑假第四周总结--开始学习Hadoop基础(三)

    简单学习云数据库系统架构(以UMP系统为例) 一.UMP系统概述 低成本和高性能的MySQL云数据库方案 二.UMP系统架构 架构设计遵循以下原则: 保持单一的系统对外入口,并且为系统内部维护单一的资 ...

  8. 剑指offer_1.24_Day_4

    构建乘积数组 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1] ...

  9. 干货|微软远程桌面服务蠕虫漏洞(CVE-2019-1182)分析

    2019年8月,微软发布了一套针对远程桌面服务的修复程序,其中包括两个关键的远程执行代码(RCE)漏洞,CVE-2019-1181和CVE-2019-1182.与之前修复的"BlueKeep ...

  10. MySQL笔记 02

    SQL对表中数据的CRUD操作: 插入数据: insert into 表名 (列名1,列名2,....) values (值1,值2,....): 插入部分: insert into xuesheng ...