[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 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
随机推荐
- maven手动安装ojdbc6.jar包到本地仓库
需要jar文件 ojdbc6.jar jar下载地址1 下载地址2 本地执行: mvn install:install-file -Dfile=D:/ojdbc6.jar -DgroupId=co ...
- Docker部署Python应用程序
Docker部署Python应用程序 1. 单个py文件部署 生成Dockerfile 文件 插件用的豆瓣的镜像,,重置时间(容器的默认时间是UTC时间与宿主机的相差8小时). 文中需要三个插件(pe ...
- MongoDB七-运维技术
复制来自:http://www.cnblogs.com/huangxincheng/archive/2012/03/08/2384571.html 这一篇我们以管理员的视角来看mongodb,作为一名 ...
- 18 react react-redux 的编写 TodoList
1. 安装 react-redux yarn add react-redux 2. react-redux 编写 TodoList 使所有子组件 都能使用 store #index.js import ...
- .NET CORE AutoMapper使用
1.通过nuget安装AutoMapper,版本是7.0.1, 安装AutoMapper.Extensions.Microsoft.DependencyInjection 版本是4.0.1 不是以上 ...
- 在div中注入html代码
直接开始: <div id="content"class="modal-body"> </div> 在angularjs中使用如下代码, ...
- RN命令的使用
RN中文网站 https://reactnative.cn/docs/getting-started/ 创建项目 1.最新版本项目react-native init MyApp 使用可行版本 rea ...
- Mac OS 终端 iTerm2配置大全
转载链接:https://www.cnblogs.com/diyxiaoshitou/p/9017413.html,在按照原文执行时发现有些问题,所以本文对原文中存在问题的地方做了些调整. 之前一直使 ...
- io流对数据的读写
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...
- 201812-2 小明放学 Java
思路: 红绿灯每种灯亮划分区间,在[0,r]区间内红灯亮,在(r,g+r]区间内绿灯亮,在(r+g,r+g+y]区间内黄灯亮,在划分好区间后只需要判断当小明到达红绿灯时是哪个灯在亮,就可以判断出通过红 ...