[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 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
随机推荐
- 九十六、SAP中ALV事件之九,显示功能按钮栏中显示ALV加强工具栏
一.排查了很久,终于找到问题所在.把问题解决了,代码如下: 二.运行效果如下 三.试一试,标准功能都可以用 完美
- Flink 复杂事物处理
简介 FlinkCEP是在Flink之上实现的复杂事件处理(CEP)库. 它允许你在无界的事件流中检测事件模式,让你有机会掌握数据中重要的事项. Flink CEP 首先需要用户创建定义一个个patt ...
- 课程作业02-1-课后作业1-(1)使用组合数公式利用n!来计算
1.设计思想:运用递归阶乘的函数,依次求出n!.k!.(n-k)!,再根据组合数的公式计算(n!/(k!*(n-k)!)). 2.程序流程图: 3.源程序代码: //信1605-3 20163429 ...
- distpicker.js 根据当前位置初始化select
学习参考的地址放在最醒目的地方: https://blog.csdn.net/idea_boy/article/details/58280076 百度官方实例:http://developer.bai ...
- js原型链理解(4)-经典继承
经典继承就是组合继承,就是组合构造函数和原型链的优点混合继承. 1.避免引用类型的属性初始化 2.避免相同方法的多次初始化 function Super(name){ this.ages = [100 ...
- ES6 - 装饰器 - Decorater
注意,修饰器对类的行为的改变,是代码编译时发生的,而不是在运行时.这意味着,修饰器能在编译阶段运行代码.也就是说,修饰器本质就是编译时执行的函数. 修饰器是一个对类进行处理的函数.修饰器函 ...
- Python Learning Day6
selenium操作 点击.清除操作 from selenium import webdriver from selenium.webdriver.common.keys import Keys im ...
- UML-设计模式-本地服务容错-代理模式
在<本地服务容错-适配器+工厂模式>中,总是优先尝试本地服务.但是,有时候需要先尝试外部服务,然后才是本地服务.GoF的代理模式可以解决这个问题. 1.代理模式的一般结构 2.使用代理模式 ...
- linux_c_udp_example
udp_server #include <stdlib.h> #include <string.h> #include <unistd.h> #include &l ...
- [SUCTF 2019]EasyWeb
0x00 知识点 本题知识量巨大,把我给看傻了..盯着网上师傅们的博客看了好久.. 知识点1 构造不包含数字和字母的webshell 思路来自p牛 参考链接: https://www.leaveson ...