Skip to content

Commit 6e9c7d2

Browse files
committed
Add solution and test-cases for problem 885
1 parent 9f83439 commit 6e9c7d2

File tree

5 files changed

+72
-27
lines changed

5 files changed

+72
-27
lines changed

leetcode/801-900/0885.Spiral-Matrix-III/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [885.Spiral Matrix III][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You start at the cell `(rStart, cStart)` of an `rows x cols` grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.
5+
6+
You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all `rows * cols` spaces of the grid.
7+
8+
Return an array of coordinates representing the positions of the grid in the order you visited them.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./example_1.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: rows = 1, cols = 4, rStart = 0, cStart = 0
16+
Output: [[0,0],[0,1],[0,2],[0,3]]
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
21+
![2](./example_2.png)
1922

20-
### 思路1
21-
> ...
22-
Spiral Matrix III
23-
```go
2423
```
25-
24+
Input: rows = 5, cols = 6, rStart = 1, cStart = 4
25+
Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
26+
```
2627

2728
## 结语
2829

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(rows int, cols int, rStart int, cStart int) [][]int {
4+
ans := make([][]int, 0)
5+
steps := 1
6+
first := true
7+
end := rows * cols
8+
for end > 0 {
9+
if rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols {
10+
ans = append(ans, []int{rStart, cStart})
11+
end--
12+
}
13+
//right
14+
if !first {
15+
for s := steps - 1; s > 0 && end > 0; s-- {
16+
rStart--
17+
if rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols {
18+
ans = append(ans, []int{rStart, cStart})
19+
end--
20+
}
21+
}
22+
}
23+
for s := steps; s > 0 && end > 0; s-- {
24+
cStart++
25+
if rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols {
26+
ans = append(ans, []int{rStart, cStart})
27+
end--
28+
}
29+
}
30+
for s := steps; s > 0 && end > 0; s-- {
31+
rStart++
32+
if rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols {
33+
ans = append(ans, []int{rStart, cStart})
34+
end--
35+
}
36+
}
37+
38+
for s := steps; s > 0 && end > 0; s-- {
39+
cStart--
40+
if rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols {
41+
ans = append(ans, []int{rStart, cStart})
42+
end--
43+
}
44+
}
45+
cStart--
46+
steps += 2
47+
first = false
48+
}
49+
return ans
550
}

leetcode/801-900/0885.Spiral-Matrix-III/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
rows, cols, r, c int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, 4, 0, 0, [][]int{{0, 0}, {0, 1}, {0, 2}, {0, 3}}},
17+
{"TestCase2", 5, 6, 1, 4, [][]int{{1, 4}, {1, 5}, {2, 5}, {2, 4}, {2, 3}, {1, 3}, {0, 3}, {0, 4}, {0, 5}, {3, 5}, {3, 4}, {3, 3}, {3, 2}, {2, 2}, {1, 2}, {0, 2}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {3, 1}, {2, 1}, {1, 1}, {0, 1}, {4, 0}, {3, 0}, {2, 0}, {1, 0}, {0, 0}}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.rows, c.cols, c.r, c.c)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
26+
c.expect, got, c.rows, c.cols, c.r, c.c)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}
3.51 KB
Loading
7.86 KB
Loading

0 commit comments

Comments
 (0)