[LC] 51. N-Queens
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的更多相关文章
- Spring Boot文档
本文来自于springboot官方文档 地址:https://docs.spring.io/spring-boot/docs/current/reference/html/ Spring Boot参考 ...
- [Leetcode][Python]51: N-Queens
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 51: N-Queenshttps://oj.leetcode.com/pro ...
- CentOS7进行OpenStack(queens)最小化部署实验出现的问题与解决过程
注:此文为<OpenStack(queens)最小化搭建记录——控制与计算共两个节点>的补充 1.chrony时间同步服务搭建的时候,出现计算节点无法与控制节点同步.(controller ...
- 5-1可视化库Seabon-整体布局风格设置
In [1]: import seaborn as sns import numpy as np import matplotlib as mpl import matplotlib.pyplot a ...
- [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 ...
- LC T668笔记 & 有关二分查找、第K小数、BFPRT算法
LC T668笔记 [涉及知识:二分查找.第K小数.BFPRT算法] [以下内容仅为本人在做题学习中的所感所想,本人水平有限目前尚处学习阶段,如有错误及不妥之处还请各位大佬指正,请谅解,谢谢!] !! ...
- 记一次jdk升级引起的 Unsupported major.minor version 51.0
之前jdk 一直是1.6,tomcat 是6.x 版本,, 现在引入的新的jar, 出现 Caused by: java.lang.UnsupportedClassVersionError: org/ ...
- 编写高质量代码:改善Java程序的151个建议(第3章:类、对象及方法___建议47~51)
建议47:在equals中使用getClass进行类型判断 本节我们继续讨论覆写equals的问题,这次我们编写一个员工Employee类继承Person类,这很正常,员工也是人嘛,而且在JavaBe ...
- 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。
laviewpbt 2014.8.4 编辑 Email:laviewpbt@sina.com QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
随机推荐
- Web安全测试学习笔记 - vulhub环境搭建
Vulhub和DVWA一样,也是开源漏洞靶场,地址:https://github.com/vulhub/vulhub 环境搭建过程如下: 1. 下载和安装Ubuntu 16.04镜像,镜像地址:htt ...
- Enum应用
public enum ZDJGJD { YSZ("01",0.3,"取得预售许可"),JGFD("02",0.6,"单位结构封顶 ...
- 二十、JavaScript之对象
一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...
- 024-PHP常用字符串函数(一)
<?php $first = "abc"; $second = "aBc"; )//字串比较 { print("字符串相等:".&qu ...
- 100-PHP二维数组的元素输出三
<?php $stu=array(array(76,87,68), array(65,89,95), array(90,80,66), array(90,95,65)); //定义一个二维数组 ...
- 080-PHP的if与elseif用法
<?php /* 正确的使用方法: */ $a = 10; $b = 20; if ($a > $b): echo $a . "大于" . $b; elseif ($a ...
- Java的Regex --正则表达式
一.概述 正则表达式通常被用来对字符串提供范围性的校验.替换那些符合某个模式(规则)的文本. 正则表达式所对应的类Pattern,所有的正则表达式都是在这个类下创建的.Pattern类用于创建一个正则 ...
- Bean XML 配置(3)- 依赖注入配置
Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...
- element-ui实现自定义多个文件上传
这里强调下是:aixos是原始的,不要qs封装过的,不然不识别传值传不过去 <el-upload action="/admin/borrow/borrowEdit" list ...
- P 1026 程序运行时间
转跳点: