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的更多相关文章

  1. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  2. [LeetCode] All solution

    比较全的leetcode答案集合: kamyu104/LeetCode grandyang

  3. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

  4. Triangle LeetCode |My solution

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. 【leetcode】solution in java——Easy1

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6409067.html 1:Hamming distance The Hamming distance betw ...

  6. 【leetcode】solution in java——Easy5

    转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...

  7. 【leetcode】solution in java——Easy4

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6415011.html 16:Invert Binary Tree 此题:以根为对称轴,反转二叉树. 思路:看到 ...

  8. 【leetcode】solution in java——Easy3

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6412505.html 心得:看到一道题,优先往栈,队列,map,list这些工具的使用上面想.不要来去都是暴搜 ...

  9. 【leetcode】solution in java——Easy2

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...

随机推荐

  1. 一个JS效果竟然要研究一天,我是不是不适合做前端?

    前言 今天这篇文章的标题,显然是要搞事情.一个JS交互效果,居然花费了一天的宝贵时间才研究出来,我是不是不太适合做前端? 别急,搬好小板凳,正文从这开始- 本来今天下班回来感觉有点累,想着今天就别学了 ...

  2. display:table 表格布局

    table 布局最大的特点 1.同行等高 2.宽度自动调节   那么table-cell是不是具备这个特点呢?答案是yes,为什么呢?css中有一个有意思的规则“创建匿名表格元素”.   拿table ...

  3. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  4. maven下的sqlserver配置jar包

    看了两天的maven,开始把之前做的ssm项目搭建成maven项目,结果在sqlserver的依赖包上受阻,sqlserver需要sqljdbc4.jar包,经过一系列百度教程才得以解决,现在总结一下 ...

  5. [转载] Hadoop和Hive单机环境搭建

    转载自http://blog.csdn.net/yfkiss/article/details/7715476和http://blog.csdn.net/yfkiss/article/details/7 ...

  6. mysql数据库第三弹

    mysql数据库知识拓展 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT ...

  7. Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: Access denied

    装了fedora23后定时关机:sudo shutdown -h +60 弹出如下信息后立刻就关机了: Failed to call ScheduleShutdown in logind, proce ...

  8. ASP.NET Core教程【二】从保存数据看特有属性与服务端验证

    前文索引: 在layout.cshtml文件中,我们可以看到如下代码: <a asp-page="/Index" class="navbar-brand" ...

  9. AIO5销售发货单numeric算数溢出报错:将numeric转换成数据类型numeric时出现算数溢出错误

    问题描述: 销售发货单报错算数溢出:将numeric转换成数据类型numeric时出现算数溢出错误.具体如下图: 当销售发货单的数量为>7万时,报错 当销售发货单的数量为<7万时,单据正常 ...

  10. openGL光源概念

    1. 光照模型      环境光——经过多次反射而来的光称为环境光,无法确定其最初的方向,但当特定的光源关闭后,它们将消失. 全局环境光——每个光源都能对场景提供环境光.此外,还有一个环境光,它不来自 ...