[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 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
随机推荐
- 《新标准C++程序设计》3.3-3.4(C++学习笔记7)
1.构造函数.析构函数和变量的生存期 构造函数在对象生成时会被调用,析构函数在对象消亡时会被调用. 程序示例分析: (1) #include<iostream> using namespa ...
- Inception Score
转载 https://www.jiqizhixin.com/articles/2019-01-10-18 全面解析Inception Score原理及其局限性 https://blog.csdn ...
- Working Plan 优先队列+贪心
题目链接:http://codeforces.com/gym/101987题目描述ICPC manager plans a new project which is to be carried out ...
- js 混合构造原型 运用
1.给10个div添加点击事件 <body> <div class="sd">sdss</div> <div class="sd ...
- UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)
题意:平面上有n(n <= 1000)个点,每个点为白点或者黑点.现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大.隔板上的点可以看做是在任意一侧. 分析:枚举每个基准点i,将 ...
- 每天一点点之vue框架开发 - vue 动态替换路由(地址栏)参数
import merge from 'webpack-merge': // 修改原有参数 this.$router.push({ query:merge(this.$route.query,{'max ...
- bzoj 3876: [Ahoi2014]支线剧情
就是加一个1的下界就好了. #include<bits/stdc++.h> #define N 100005 #define LL long long #define inf 0x3f3f ...
- bash: java: command not found
[root@izm5eab8t820b79js38tbxz ~]# java -version -bash: java: command not found 出现上面问题,解决方法: [root@iz ...
- QF中间件
QF中间件使用说明 QF中间件是在2020年春节期间出现新型冠状病毒感染的肺炎疫情不敢外出,闲来无事编写的.编程是业余爱好,平时编程只会拖控件,中间件可能存在未知Bug,这个版本也只 ...
- mybatis的插入数据后的主键获取
为什么要在插入数据后获取主键:当有一个订单表和订单详情表,当插入订单表的数据后,需要在订单详情表插入该订单的具体购物情况,订单详情表需要的一个列是订单表的主键或者订单ID.(通俗讲:A表的主键是B表的 ...