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 ...
随机推荐
- PLSQL导入Excel表中数据
PL/SQL 和SQL Sever导入excel数据的原理类似,就是找到一个导入excel数据的功能项,按照步骤走就是了.下面是一个些细节过程,希望对像我这样的菜鸟有帮助. www.2cto.co ...
- POJ1067 取石子游戏
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 安装最新版本的ReSharper导致原生全局搜索工具的消失问题
经过检测,是由于启用了一个插件导致的,禁用即可,如下图:
- CoreOS Architecture Learning
目录 . CoreOS简介 . CoreOS部署.安装.使用 . CoreOS命令使用 1. CoreOS简介 0x1: CoreOS和Docker的关系 我们先来看一张Docker的架构图
- UVa OJ 197 - Cube (立方体)
Time limit: 30.000 seconds限时30.000秒 Problem问题 There was once a 3 by 3 by 3 cube built of 27 smaller ...
- [LeetCode] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- Zabbix 监控 Nginx 状态
简介: 如何使用 Zabbix 监控 Nginx 状态 ? 1.获取 Nginx 状态( HTTP Stub Status ) shell > /usr/local/nginx/sbin/ngi ...
- mysql 出现错误Incorrect file format
REPAIR TABLE xx_messages USE_FRM;
- Memcached在windows下的安装于使用
原文链接:http://blog.csdn.net/jjmaiz/article/details/7935672 有一点要注意的是,上文作者没有提及: 将php_memcached.dll放在ext文 ...
- prob
void calc_probability(int num) { , j = , k = ; #define SIZE_NUM 8 int *array_num = NULL; int *rememb ...