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 ...
随机推荐
- 一键生成koa/koa2项目:
一键生成koa/koa2项目: 1. npm install -g koa-generator 2.新建项目目录 koa mytest (koa1项目) koa2 koa2test (koa2项目) ...
- 利用HTML5新特性改变浏览器地址后不刷新页面
原文:http://www.cnblogs.com/xuchengzone/archive/2013/04/18/html5-history-pushstate.html 作为一个程序员,上Git ...
- cloneNode克隆节点在不同浏览器的差异
cloneNode是用于克隆节点的,如果待克隆的节点还有子节点以及自定义属性.添加的有事件,那么克隆时,可以指定是克隆节点本身,还是将其所有子节点信息也克隆进去,这是通过给cloneNode传递一个布 ...
- Oracle-3 - :超级适合初学者的入门级笔记--用户权限,set运算符,高级子查询
上一篇的内容在这里第二篇内容, 用户权限:创建用户,创建角色,使用grant 和 revoke 语句赋予和回收权限,创建数据库联接 创建用户:create user xxx identified b ...
- 【译】Asp.Net Identity Cookies 格式化-中英对照版
原文出处 Trailmax Tech Max Vasilyev: ASP.Net MVC development in Aberdeen, Scotland I've been reached out ...
- 借助 frp 随时随地访问自己的树莓派
前言 看了知乎上的一个「树莓派」是什么以及普通人怎么玩? 的高票回答,双十一时间,果断买了一个树莓派 3. 周一(11.13) 到的货.我目前只想实现一个简单的功能 -- 想从任意位置访问我的树莓派. ...
- [转载] 应用于负载均衡的一致性哈希及java实现
转载自http://blog.csdn.net/haitao111313/article/details/7537799 这几天看了几遍一致性哈希的文章,但是都没有比较完整的实现,因此试着实现了一下, ...
- Python机器学习库和深度学习库总结
我们在Github上的贡献者和提交者之中检查了用Python语言进行机器学习的开源项目,并挑选出最受欢迎和最活跃的项目. 1. Scikit-learn(重点推荐) www.github.com/sc ...
- 容易被忽视的后端服务 chunked 性能问题
容易被忽视的后端服务 chunked 性能问题 标签(空格分隔): springboot springmvc chunked 背景 spring boot 创建的默认 spring mvc 项目 集成 ...
- C++雾中风景3:const用法的小结
const作为C与C++共有的关键字,很多使用的方式大同小异.但由于C++是一门面向对象的语言,在类和对象中有更多的使用规则.之前学习C语言的时候就被const这个关键字搅得焦头烂额,正巧也借这篇文章 ...