Skip to content

Commit 8eb2c3a

Browse files
refactor 51
1 parent e5a110a commit 8eb2c3a

File tree

1 file changed

+50
-73
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+50
-73
lines changed

src/main/java/com/fishercoder/solutions/_51.java

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,68 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 51. N-Queens
8-
*
9-
* The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
10-
11-
Given an integer n, return all distinct solutions to the n-queens puzzle.
12-
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
13-
14-
For example,
15-
There exist two distinct solutions to the 4-queens puzzle:
16-
17-
[
18-
[".Q..", // Solution 1
19-
"...Q",
20-
"Q...",
21-
"..Q."],
22-
23-
["..Q.", // Solution 2
24-
"Q...",
25-
"...Q",
26-
".Q.."]
27-
]
28-
*/
296
public class _51 {
307

31-
public static class Solution1 {
8+
public static class Solution1 {
329

33-
public List<List<String>> solveNQueens(int n) {
34-
List<List<String>> result = new ArrayList<>();
35-
if (n <= 0) {
36-
return result;
37-
}
38-
search(n, new ArrayList<>(), result);
39-
return result;
40-
}
10+
public List<List<String>> solveNQueens(int n) {
11+
List<List<String>> result = new ArrayList<>();
12+
if (n <= 0) {
13+
return result;
14+
}
15+
search(n, new ArrayList<>(), result);
16+
return result;
17+
}
4118

42-
private void search(int n, ArrayList<Integer> col, List<List<String>> result) {
43-
if (col.size() == n) {
44-
result.add(drawChessBoard(col));
45-
return;
46-
}
19+
private void search(int n, ArrayList<Integer> col, List<List<String>> result) {
20+
if (col.size() == n) {
21+
result.add(drawChessBoard(col));
22+
return;
23+
}
4724

48-
for (int i = 0; i < n; i++) {
49-
if (!isValid(col, i)) {
50-
continue;
25+
for (int i = 0; i < n; i++) {
26+
if (!isValid(col, i)) {
27+
continue;
28+
}
29+
col.add(i);
30+
search(n, col, result);
31+
col.remove(col.size() - 1);
32+
}
5133
}
52-
col.add(i);
53-
search(n, col, result);
54-
col.remove(col.size() - 1);
55-
}
56-
}
5734

58-
private boolean isValid(ArrayList<Integer> col, int next) {
59-
int row = col.size();
60-
for (int i = 0; i < row; i++) {
61-
if (next == col.get(i)) {
62-
return false;
63-
}
35+
private boolean isValid(ArrayList<Integer> col, int next) {
36+
int row = col.size();
37+
for (int i = 0; i < row; i++) {
38+
if (next == col.get(i)) {
39+
return false;
40+
}
6441

65-
if (i - row == col.get(i) - next) {
66-
return false;
67-
}
42+
if (i - row == col.get(i) - next) {
43+
return false;
44+
}
6845

69-
if (i - row == next - col.get(i)) {
70-
return false;
46+
if (i - row == next - col.get(i)) {
47+
return false;
48+
}
49+
}
50+
return true;
7151
}
72-
}
73-
return true;
74-
}
7552

76-
private ArrayList<String> drawChessBoard(ArrayList<Integer> col) {
77-
ArrayList<String> chessBoard = new ArrayList<>();
53+
private ArrayList<String> drawChessBoard(ArrayList<Integer> col) {
54+
ArrayList<String> chessBoard = new ArrayList<>();
7855

79-
for (int i = 0; i < col.size(); i++) {
80-
String row = "";
81-
for (int j = 0; j < col.size(); j++) {
82-
if (col.get(j) == i) {
83-
row += "Q";
84-
} else {
85-
row += ".";
86-
}
56+
for (int i = 0; i < col.size(); i++) {
57+
String row = "";
58+
for (int j = 0; j < col.size(); j++) {
59+
if (col.get(j) == i) {
60+
row += "Q";
61+
} else {
62+
row += ".";
63+
}
64+
}
65+
chessBoard.add(row);
66+
}
67+
return chessBoard;
8768
}
88-
chessBoard.add(row);
89-
}
90-
return chessBoard;
9169
}
92-
}
9370
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy