LeetCode :My solution N-Queens
N-Queens
Total Accepted: 15603 Total
Submissions: 60198My Submissions
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

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.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."], ["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
public class Solution {
public List<String[]> solveNQueens(int n) {
List<String[]> result = new ArrayList<String[]>();
if ( n < 1) {
return result ;
}
List<List<Integer>> storeArray = new ArrayList<List<Integer>>();
helper(n, new ArrayList<Integer>(), storeArray);
result = drawPicture(storeArray,n);
return result;
}
private List<String[]> drawPicture(List<List<Integer>> storeArray ,int n) {
List<String[]> drawedPicture = new ArrayList<String[]> ();
for (int i = 0; i < storeArray.size();i++) {
String[] str = new String[n];
for (int j = 0; j < n; j++) {
str[j] = new String("");
for (int w = 0; w < n; w++) {
if (storeArray.get(i).get(j) == w) {
str[j] +="Q";
} else {
str[j] +=".";
}
}
}
drawedPicture.add(str);
}
return drawedPicture;
}
boolean isValid(ArrayList<Integer> cols, int col) {
int newX = col;
int newY = cols.size();
for (int y = 0; y < cols.size(); y++) {
int x = cols.get(y);
if (newX == x) {
return false;
}
if (newX - newY == x - y) {
return false;
}
if (newX + newY == x + y){
return false;
}
}
return true;
}
public void helper(int n, ArrayList<Integer> cols, List<List<Integer>> storeArray) {
if (cols.size() == n) {
storeArray.add(new ArrayList<Integer>(cols));
return;
}
for (int i = 0; i < n; i++) {
if (!isValid(cols, i)) {
continue;
}
cols.add(i);
helper(n, cols, storeArray);
cols.remove(cols.size() - 1);
}
}
}
LeetCode :My solution N-Queens的更多相关文章
- Leetcode Python Solution(continue update)
leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...
- [LeetCode] All solution
比较全的leetcode答案集合: kamyu104/LeetCode grandyang
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- Triangle LeetCode |My solution
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】solution in java——Easy1
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6409067.html 1:Hamming distance The Hamming distance betw ...
- 【leetcode】solution in java——Easy5
转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...
- 【leetcode】solution in java——Easy4
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6415011.html 16:Invert Binary Tree 此题:以根为对称轴,反转二叉树. 思路:看到 ...
- 【leetcode】solution in java——Easy3
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6412505.html 心得:看到一道题,优先往栈,队列,map,list这些工具的使用上面想.不要来去都是暴搜 ...
- 【leetcode】solution in java——Easy2
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...
随机推荐
- XMLHttpRequest2 异步 ajax
XMLHttpRequest1只是对已经存在的xhr对象细节进行规范定义, XMLHttpRequest2升级了该对象. FormData 类型可以用在xhr传输的时候,把表单序列化或者将数据以表 ...
- 服务器 : Apache Tomcat - 理解架构层次
文章概览 相信很多接触java的人都对Tom猫有着多少的熟悉,就个人而言,本来只知道Tom简单的操作与配置,就像裹上一层纱,迷迷糊糊的. Tomcat的书籍本来就不多,高分的还是很久之前的版本,直到最 ...
- J2EE--常见面试题总结 -- 一
StringBuilder和StringBuffer的区别: String 字符串常量 不可变 使用字符串拼接时是不同的2个空间 StringBuffer 字符串变量 可变 ...
- C++雾中风景2:struct还是class?
之前因为都在忙着毕业的开题答辩与投稿论文的事宜,一直没有时间更新这个系列的文章.师弟看了上一篇雾中风景的文章,希望我继续把这个系列的文章写下去.坦白说,C++的特性很多,这也不是教学指南的文章,我会选 ...
- Map的遍历方法(java)
方法一.Set<Object> keySet();返回集合中所有的key组成的集合. 代码:Map<String , String > map=new HashMap();f ...
- DNS生效时间
http://blog.itechol.com/space-33-do-blog-id-908.html http://www.madboa.com/geek/dig/ Dig简介 Dig是一个在类U ...
- 将传统项目改造为SSM框架的项目
首先 第一步改变传统dao层 先要再resource文件夹下创建一个applicationContext.xml 内容如下 关键代码 <!-- 使spring扫描包下的所 ...
- JAVA基础面试(二)
11.是否可以从一个static方法内部发出对非static方法的调用? 不可以.因为非static方法是要与对象关联在一起的,必须创建一个对象后,才可以在该对象上进行方法调用,而static方法调用 ...
- 基于Jquery UI的autocompelet改写,自动补全控件,增加下拉选项,动态设置样式,点击显示所有选项,并兼容ie6+
Jquery UI的autocompelete改写 注意:实现功能,除了原版的自动补全内容外,增加一个点击显示所有选项,样式能动态设置. 加载数据的来源为后台数据库读取. 具体代码如下: 引用 从官方 ...
- 机器学习(二)-kNN手写数字识别
一.kNN算法是机器学习的入门算法,其中不涉及训练,主要思想是计算待测点和参照点的距离,选取距离较近的参照点的类别作为待测点的的类别. 1,距离可以是欧式距离,夹角余弦距离等等. 2,k值不能选择太大 ...