Java for LeetCode 051 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.
解题思路:经典的八皇后问题,由于之前在《JAVA语言程序设计》中exerice6-22中做过这种问题,代码直接拿来修改下即可,JAVA实现如下:
static public List<String[]> solveNQueens(int n) {
List<String[]> list=new ArrayList<String[]>();
if(n==1){
String[] str={"Q"};
list.add(str);
return list;
}
// int count = 0;
int[] queens = new int[n]; // queens are placed at (i, queens[i])
for (int i = 0; i < n; i++)
queens[i] = -1;
queens[0] = 0;
int k = 1;
while (k >=0) {
int j = findPosition(k, queens,n);
if (j ==-1) {
queens[k] = -1;
k--; // back track to the previous row
} else {
queens[k] = j;
if (k == n-1) {
// count++;
String[] queenArray=new String[n];
for(int i=0;i<n;i++){
StringBuilder sb=new StringBuilder();
for(int j2=0;j2<n;j2++){
if(j2==queens[i])
sb.append('Q');
else sb.append('.');
}
queenArray[i]=new String(sb);
}
list.add(queenArray);
}
else {
k++;
}
}
}
return list;
}
public static int findPosition(int k, int[] queens,int n) {
int start = queens[k] == -1 ? 0 : queens[k] + 1;
for (int j = start; j < n; j++) {
if (isValid(k, j, queens,n))
return j;
}
return -1;
} public static boolean isValid(int k, int j, int queens[],int n) {
for (int i = 0; i < k; i++)
if (queens[i] == j)
return false;
for (int row = k - 1, column = j - 1; row >= 0 && column >= 0; row--, column--)
if (queens[row] == column)
return false;
for (int row = k - 1, column = j + 1; row >= 0 && column <= n-1; row--, column++)
if (queens[row] == column)
return false;
return true;
}
Java for LeetCode 051 N-Queens的更多相关文章
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Java for LeetCode 153 Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- ORACLE数据库删除表中记录报record is locked by another user
在操作ORACLE数据库的时候,由于执行完,没有COMMIT,直接把PL/SQL关闭掉,后来导致那张表被锁住,当编辑时就会出现这个信息,record is locked by another user ...
- 内部类访问局部变量的时候,为什么变量必须加上final修饰
这里的局部变量就是在类方法中的变量,能访问方法中变量的类当然也是局部内部类了.我们都知道,局部变量在所处的函数执行完之后就释放了,但是内部类对象如果还有引用指向的话它是还存在的.例如下面的代码: cl ...
- Laravel 5 中的配置
介绍 Laravel 的所有的配置文件都放在了 config 这个目录的下面.每个选项都有介绍. config├── app.php├── auth.php├── cache.php├── compi ...
- kindeditor粘贴word文档内容时去除格式的方法?如何设置为默认无文本格式呢?
打开文件夹找到kindeditor-min.js文件,搜索pasteType函数,默认值是2.设置为1即可. 设置粘贴类型,0:禁止粘贴, 1:纯文本粘贴, 2:HTML粘贴.
- CentOS 配置hadoop
Hadoop是用作处理大数据用的,核心是HDFS.Map/Reduce.虽然目前工作中不需要使用这个,但是,技多不压身,经过虚拟机很多遍的尝试,终于将Hadoop2.5.2的环境顺利搭建起来了. ...
- php统计字数函数
function countWords($str){ echo (mb_strlen($str, 'utf8') + strlen($str))/2; }
- DataTable中如何去除重复的项
DataView dv =dataTable.DefaultView; DataTable dt = dv.ToTable(true, "Name");
- struts2拦截器interceptor的三种配置方法
1.struts2拦截器interceptor的三种配置方法 方法1. 普通配置法 <struts> <package name="struts2" extend ...
- java本地方法如何调用其他程序函数,方法详解2
Java调用本地方法(JNI浅谈) (2006-11-27 14:55:36) 转载▼ 分类: Java类文章 本人在项目开发实践中的总结和体会 前段时间公司 ...
- javascript模板方法模式
一:什么是模板方法模式: 模板方法模式由二部分组成,第一部分是抽象父类,第二部分是具体实现的子类,一般的情况下是抽象父类封装了子类的算法框架,包括实现一些公共方法及封装子类中所有方法的执行顺序,子类可 ...