Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit d415886

Browse files
aQuaaQua
authored andcommitted
542 added
1 parent fc7c61e commit d415886

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Problem0542
2+
3+
func updateMatrix(matrix [][]int) [][]int {
4+
res :=
5+
6+
return res
7+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package Problem0542
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
matrix [][]int
13+
ans [][]int
14+
}{
15+
16+
{
17+
[][]int{
18+
{0, 0, 0},
19+
{0, 1, 0},
20+
{0, 0, 0},
21+
},
22+
[][]int{
23+
{0, 0, 0},
24+
{0, 1, 0},
25+
{0, 0, 0},
26+
},
27+
},
28+
29+
{
30+
[][]int{
31+
{0, 0, 0},
32+
{0, 1, 0},
33+
{1, 1, 1},
34+
},
35+
[][]int{
36+
{0, 0, 0},
37+
{0, 1, 0},
38+
{1, 2, 1},
39+
},
40+
},
41+
42+
// 可以有多个 testcase
43+
}
44+
45+
func Test_updateMatrix(t *testing.T) {
46+
ast := assert.New(t)
47+
48+
for _, tc := range tcs {
49+
fmt.Printf("~~%v~~\n", tc)
50+
ast.Equal(tc.ans, updateMatrix(tc.matrix), "输入:%v", tc)
51+
}
52+
}
53+
54+
func Benchmark_updateMatrix(b *testing.B) {
55+
for i := 0; i < b.N; i++ {
56+
for _, tc := range tcs {
57+
updateMatrix(tc.matrix)
58+
}
59+
}
60+
}

Algorithms/0542.01-matrix/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [542. 01 Matrix](https://leetcode.com/problems/01-matrix/)
2+
3+
## 题目
4+
5+
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
6+
7+
The distance between two adjacent cells is 1.
8+
9+
Example 1:
10+
11+
```text
12+
Input:
13+
0 0 0
14+
0 1 0
15+
0 0 0
16+
17+
Output:
18+
0 0 0
19+
0 1 0
20+
0 0 0
21+
```
22+
23+
Example 2:
24+
25+
```text
26+
Input:
27+
0 0 0
28+
0 1 0
29+
1 1 1
30+
31+
Output:
32+
0 0 0
33+
0 1 0
34+
1 2 1
35+
```
36+
37+
Note:
38+
39+
1. The number of elements of the given matrix will not exceed 10,000.
40+
1. There are at least one 0 in the given matrix.
41+
1. The cells are adjacent in only four directions: up, down, left and right.
42+
43+
## 解题思路
44+
45+
见程序注释

0 commit comments

Comments
 (0)