Skip to content

feat: add solutions to lc problem: No.0542 #1613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/compress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
id: calibre
uses: calibreapp/image-actions@main
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
githubToken: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }}
# For non-Pull Requests, run in compressOnly mode and we'll PR after.
compressOnly: ${{ github.event_name != 'pull_request' }}
- name: Create Pull Request
Expand Down
13 changes: 9 additions & 4 deletions .github/workflows/pr-add-label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ name: pr-add-label

on:
pull_request:
types:
- opened
- synchronize
types: [opened]

permissions:
contents: read

jobs:
add-label:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Check PR number
Expand All @@ -16,7 +21,7 @@ jobs:
- name: Run add-label Action
uses: thinkasany/pr-label-action@master
with:
github_token: ${{ secrets.ACTION_TOKEN }}
github_token: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }}
pr_number: ${{ env.PR_NUMBER }}
organize_name: "doocs"
team_name: "leetcode-algorithm"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Pull request
uses: actions-cool/maintain-one-comment@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
token: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }}
body: |
🤭 感谢你的提交,请检查你的改动是否符合以下项目规范。

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/starcharts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: MaoLongLong/actions-starcharts@main
with:
github_token: ${{ secrets.ACTION_TOKEN }}
github_token: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }}
svg_path: images/starcharts.svg
commit_message: "chore: auto update starcharts"
stars_change: ${{ secrets.STARS_CHANGE }}
59 changes: 44 additions & 15 deletions solution/0500-0599/0542.01 Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ class Solution:
ans = [[-1] * n for _ in range(m)]
q = deque()
for i, row in enumerate(mat):
for j, v in enumerate(row):
if v == 0:
for j, x in enumerate(row):
if x == 0:
ans[i][j] = 0
q.append((i, j))
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
dirs = (-1, 0, 1, 0, -1)
while q:
i, j = q.popleft()
for a, b in dirs:
for a, b in pairwise(dirs):
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
ans[x][y] = ans[i][j] + 1
Expand All @@ -87,30 +87,29 @@ class Solution:

```java
class Solution {

public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[][] ans = new int[m][n];
for (int i = 0; i < m; ++i) {
Arrays.fill(ans[i], -1);
for (int[] row : ans) {
Arrays.fill(row, -1);
}
Deque<int[]> q = new LinkedList<>();
Deque<int[]> q = new ArrayDeque<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 0) {
ans[i][j] = 0;
q.offer(new int[] {i, j});
ans[i][j] = 0;
}
}
}
int[] dirs = new int[] {-1, 0, 1, 0, -1};
int[] dirs = {-1, 0, 1, 0, -1};
while (!q.isEmpty()) {
int[] t = q.poll();
for (int i = 0; i < 4; ++i) {
int x = t[0] + dirs[i];
int y = t[1] + dirs[i + 1];
int[] p = q.poll();
int i = p[0], j = p[1];
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
ans[x][y] = ans[t[0]][t[1]] + 1;
ans[x][y] = ans[i][j] + 1;
q.offer(new int[] {x, y});
}
}
Expand Down Expand Up @@ -244,6 +243,36 @@ func updateMatrix(mat [][]int) [][]int {
}
```

### **TypeScript**

```ts
function updateMatrix(mat: number[][]): number[][] {
const [m, n] = [mat.length, mat[0].length];
const ans: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => -1));
const q: [number, number][] = [];
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (mat[i][j] === 0) {
q.push([i, j]);
ans[i][j] = 0;
}
}
}
const dirs: number[] = [-1, 0, 1, 0, -1];
while (q.length) {
const [i, j] = q.shift()!;
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] === -1) {
ans[x][y] = ans[i][j] + 1;
q.push([x, y]);
}
}
}
return ans;
}
```

### **...**

```
Expand Down
59 changes: 44 additions & 15 deletions solution/0500-0599/0542.01 Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class Solution:
ans = [[-1] * n for _ in range(m)]
q = deque()
for i, row in enumerate(mat):
for j, v in enumerate(row):
if v == 0:
for j, x in enumerate(row):
if x == 0:
ans[i][j] = 0
q.append((i, j))
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
dirs = (-1, 0, 1, 0, -1)
while q:
i, j = q.popleft()
for a, b in dirs:
for a, b in pairwise(dirs):
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
ans[x][y] = ans[i][j] + 1
Expand All @@ -79,30 +79,29 @@ class Solution:

```java
class Solution {

public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[][] ans = new int[m][n];
for (int i = 0; i < m; ++i) {
Arrays.fill(ans[i], -1);
for (int[] row : ans) {
Arrays.fill(row, -1);
}
Deque<int[]> q = new LinkedList<>();
Deque<int[]> q = new ArrayDeque<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 0) {
ans[i][j] = 0;
q.offer(new int[] {i, j});
ans[i][j] = 0;
}
}
}
int[] dirs = new int[] {-1, 0, 1, 0, -1};
int[] dirs = {-1, 0, 1, 0, -1};
while (!q.isEmpty()) {
int[] t = q.poll();
for (int i = 0; i < 4; ++i) {
int x = t[0] + dirs[i];
int y = t[1] + dirs[i + 1];
int[] p = q.poll();
int i = p[0], j = p[1];
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
ans[x][y] = ans[t[0]][t[1]] + 1;
ans[x][y] = ans[i][j] + 1;
q.offer(new int[] {x, y});
}
}
Expand Down Expand Up @@ -236,6 +235,36 @@ func updateMatrix(mat [][]int) [][]int {
}
```

### **TypeScript**

```ts
function updateMatrix(mat: number[][]): number[][] {
const [m, n] = [mat.length, mat[0].length];
const ans: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => -1));
const q: [number, number][] = [];
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (mat[i][j] === 0) {
q.push([i, j]);
ans[i][j] = 0;
}
}
}
const dirs: number[] = [-1, 0, 1, 0, -1];
while (q.length) {
const [i, j] = q.shift()!;
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] === -1) {
ans[x][y] = ans[i][j] + 1;
q.push([x, y]);
}
}
}
return ans;
}
```

### **...**

```
Expand Down
20 changes: 10 additions & 10 deletions solution/0500-0599/0542.01 Matrix/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[][] ans = new int[m][n];
for (int i = 0; i < m; ++i) {
Arrays.fill(ans[i], -1);
for (int[] row : ans) {
Arrays.fill(row, -1);
}
Deque<int[]> q = new LinkedList<>();
Deque<int[]> q = new ArrayDeque<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 0) {
ans[i][j] = 0;
q.offer(new int[] {i, j});
ans[i][j] = 0;
}
}
}
int[] dirs = new int[] {-1, 0, 1, 0, -1};
int[] dirs = {-1, 0, 1, 0, -1};
while (!q.isEmpty()) {
int[] t = q.poll();
for (int i = 0; i < 4; ++i) {
int x = t[0] + dirs[i];
int y = t[1] + dirs[i + 1];
int[] p = q.poll();
int i = p[0], j = p[1];
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
ans[x][y] = ans[t[0]][t[1]] + 1;
ans[x][y] = ans[i][j] + 1;
q.offer(new int[] {x, y});
}
}
Expand Down
10 changes: 5 additions & 5 deletions solution/0500-0599/0542.01 Matrix/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
m, n = len(mat), len(mat[0])
ans = [[-1] * n for _ in range(m)]
q = deque()
for i in range(m):
for j in range(n):
if mat[i][j] == 0:
for i, row in enumerate(mat):
for j, x in enumerate(row):
if x == 0:
ans[i][j] = 0
q.append((i, j))
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
dirs = (-1, 0, 1, 0, -1)
while q:
i, j = q.popleft()
for a, b in dirs:
for a, b in pairwise(dirs):
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
ans[x][y] = ans[i][j] + 1
Expand Down
25 changes: 25 additions & 0 deletions solution/0500-0599/0542.01 Matrix/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function updateMatrix(mat: number[][]): number[][] {
const [m, n] = [mat.length, mat[0].length];
const ans: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => -1));
const q: [number, number][] = [];
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (mat[i][j] === 0) {
q.push([i, j]);
ans[i][j] = 0;
}
}
}
const dirs: number[] = [-1, 0, 1, 0, -1];
while (q.length) {
const [i, j] = q.shift()!;
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] === -1) {
ans[x][y] = ans[i][j] + 1;
q.push([x, y]);
}
}
}
return ans;
}