Skip to content

Commit

Permalink
Merge pull request #596 from 0xff-dev/542
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 542
  • Loading branch information
6boris authored Aug 22, 2023
2 parents 9219ac4 + a7de3e9 commit ebfa192
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
Binary file added leetcode/501-600/0542.01-Matrix/01-1-grid.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/501-600/0542.01-Matrix/01-2-grid.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 13 additions & 14 deletions leetcode/501-600/0542.01-Matrix/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [542.01 Matrix][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Given an `m x n` binary matrix `mat`, return the distance of the nearest `0` for each cell.

The distance between two adjacent cells is `1`.

**Example 1:**

**Example 1:**
![example1](./01-1-grid.jpeg)

```
Input: a = "11", b = "1"
Output: "100"
Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0],[0,0,0]]
```

## 题意
> ...
**Example 2:**

## 题解
![example2](./01-2-grid.jpeg)

### 思路1
> ...
01 Matrix
```go
```

Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0],[0,1,0],[1,2,1]]
```

## 结语

Expand Down
36 changes: 34 additions & 2 deletions leetcode/501-600/0542.01-Matrix/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package Solution

func Solution(x bool) bool {
return x
import "math"

func Solution(mat [][]int) [][]int {
rows := len(mat)
cols := len(mat[0])
zeroPoints := make([][2]int, 0)
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
if mat[r][c] == 0 {
zeroPoints = append(zeroPoints, [2]int{r, c})
continue
}
mat[r][c] = math.MaxInt

Check failure on line 15 in leetcode/501-600/0542.01-Matrix/Solution.go

View workflow job for this annotation

GitHub Actions / Test Solution Cases

undefined: math.MaxInt
}
}
var dirs = [][]int{
{1, 0}, {0, 1}, {-1, 0}, {0, -1},
}
for len(zeroPoints) > 0 {
next := make([][2]int, 0)
for _, item := range zeroPoints {
for _, dir := range dirs {
nx, ny := item[0]+dir[0], item[1]+dir[1]
if nx < 0 || nx >= rows || ny < 0 || ny >= cols || mat[nx][ny] <= mat[item[0]][item[1]]+1 {
continue
}
mat[nx][ny] = mat[item[0]][item[1]] + 1
next = append(next, [2]int{nx, ny})
}
}
zeroPoints = next
}
return mat

}
14 changes: 7 additions & 7 deletions leetcode/501-600/0542.01-Matrix/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {0, 0, 0}},
[][]int{{19, 19, 19}, {18, 18, 18}, {17, 17, 17}, {16, 16, 16}, {15, 15, 15}, {14, 14, 14}, {13, 13, 13}, {12, 12, 12}, {11, 11, 11}, {10, 10, 10}, {9, 9, 9}, {8, 8, 8}, {7, 7, 7}, {6, 6, 6}, {5, 5, 5}, {4, 4, 4}, {3, 3, 3}, {2, 2, 2}, {1, 1, 1}, {0, 0, 0}}},
{"TestCase2", [][]int{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}, [][]int{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit ebfa192

Please sign in to comment.