Skip to content

Commit fdce46e

Browse files
committed
✨ Add solution and test-cases for Problem 51
1 parent cb0f1de commit fdce46e

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed
Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(x int) [][]string {
6+
board := make([][]string, x)
7+
for i := range board {
8+
board[i] = make([]string, x)
9+
for j := range board[i] {
10+
board[i][j] = ".";
11+
}
12+
}
13+
res := make([][]string, 0)
14+
helper(board, &res, 0);
15+
return res
16+
}
17+
18+
func helper(board [][]string, res *[][]string, row int) {
19+
if len(board) == row {
20+
temp := make([]string, 0);
21+
for i := range board {
22+
temp = append(temp, strings.Join(board[i], ""));
23+
}
24+
*res = append(*res, temp);
25+
return;
26+
}
27+
for col := 0; col < len(board); col++ {
28+
if isValid(board, row, col) {
29+
board[row][col] = "Q";
30+
helper(board, res, row + 1);
31+
board[row][col] = ".";
32+
}
33+
}
34+
}
35+
36+
func isValid(board [][]string, r , c int) bool {
37+
for i := 0; i < r; i++ {
38+
if board[i][c] == "Q" {
39+
return false;
40+
}
41+
}
42+
for i, j := r, c; i >= 0 && j >= 0; i, j = i - 1, j - 1 {
43+
if board[i][j] == "Q" {
44+
return false;
45+
}
46+
}
47+
for i, j := r, c; i >= 0 && j < len(board); i, j = i - 1, j + 1 {
48+
if board[i][j] == "Q" {
49+
return false;
50+
}
51+
}
52+
return true;
553
}

leetcode/1-100/0051.N-Queens/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@ func TestSolution(t *testing.T) {
99
// 测试用例
1010
cases := []struct {
1111
name string
12-
inputs bool
13-
expect bool
12+
x int
13+
expect [][]string
1414
}{
15-
{"TestCacse 1", true, true},
16-
{"TestCacse 1", true, true},
17-
{"TestCacse 1", false, false},
15+
{"TestCase", 4, [][]string{{".Q..","...Q","Q...","..Q."},{"..Q.","Q...","...Q",".Q.."}}},
16+
{"TestCase", 1, [][]string{{"Q"}}},
1817
}
1918

2019
// 开始测试
2120
for _, c := range cases {
2221
t.Run(c.name, func(t *testing.T) {
23-
ret := Solution(c.inputs)
22+
ret := Solution(c.x)
2423
if !reflect.DeepEqual(ret, c.expect) {
2524
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect, ret, c.inputs)
25+
c.expect, ret, c.x)
2726
}
2827
})
2928
}

leetcode/1-100/0051.n-queens.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22

33
## Description
44

5-
Given two binary strings, return their sum \(also a binary string\).
5+
The **n-queens** puzzle is the problem of placing `n` queens on an `n` x `n` chessboard such that no two queens attack each other.
66

7-
The input strings are both **non-empty** and contains only characters `1` or `0`.
7+
Given an integer `n`, return all distinct solutions to the **n-queens puzzle**. You may return the answer in **any order**.
8+
9+
Each solution contains a distinct board configuration of the n-queens' placement, where `'Q'` and `'.'` both indicate a queen and an empty space, respectively.
810

911
**Example 1:**
1012

1113
```text
12-
Input: a = "11", b = "1"
13-
Output: "100"
14+
Input: n = 4
15+
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
1416
```
1517

1618
**Example 2:**
1719

1820
```text
19-
Input: a = "1010", b = "1011"
20-
Output: "10101"
21+
Input: n = 1
22+
Output: [["Q"]]
2123
```
2224

23-
**Tags:** Math, String
25+
**Tags:** Array, Backtracking
2426

2527
## 题意
2628

0 commit comments

Comments
 (0)