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 ...
随机推荐
- cookie在不同域名domain、path下的读取规则
cookie 子域名可以读父域名中的cookie 如在 .ping.com域下注入cookie,则该子域下的网页如p1.ping.com.p2.ping.com 都能读取到cookie信息 path的 ...
- Java面试题-2
程序员面试之九阴真经 谈谈final, finally, finalize的区别: final:::修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此 ...
- ArrayBuffer and Base64
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- 10.javaweb核心标签库详解
一.JSTL简介及在项目中安装配置 1, 简介 使用JSTL标签的目的就是不希望jsp中出现java逻辑代码 分类 2, JSTL的安装配置 首先将jar包中的各个标签库配置文件拷贝到项目WEB- ...
- c#读取Excel数据到Gridview
#region 读取Excel数据到Gridview public void ReadExcel(string sExcelFile, GridView dgBom) { DataTable E ...
- Docker Swarm 中最重要的概念- 每天5分钟玩转 Docker 容器技术(94)
从主机的层面来看,Docker Swarm 管理的是 Docker Host 集群.所以先来讨论一个重要的概念 - 集群化(Clustering). 服务器集群由一组网络上相互连接的服务器组成,它们一 ...
- [转载] zookeeper工作原理、安装配置、工具命令简介
转载自http://www.cnblogs.com/kunpengit/p/4045334.html 1 Zookeeper简介Zookeeper 是分布式服务框架,主要是用来解决分布式应用中经常遇到 ...
- Getting Started With setuptools and setup.py
https://pythonhosted.org/an_example_pypi_project/setuptools.html http://www.ianbicking.org/docs/setu ...
- innobackupex: fatal error: no ‘innodb_buffer_pool_filename’解决方法
http://www.ttlsa.com/mysql/innobackupex-1-5-1-fatal-error-no-innodb_buffer_pool_filename/
- python变量命名规则
在Python中使用变量时,需要遵守一些规则和指南.违反这些规则将引发错误,而指南旨在让你编写的代码更容易阅读和理解.请务必牢记下述有关变量的规则. 变量名只能包含字母.数字和下划线.变量名可以字 ...