Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d484bea

Browse files
feat: add solutions to lc problem: No.0542 (#1613)
No.0542.01 Matrix
1 parent 6f950c9 commit d484bea

File tree

9 files changed

+140
-52
lines changed

9 files changed

+140
-52
lines changed

‎.github/workflows/compress.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
id: calibre
3535
uses: calibreapp/image-actions@main
3636
with:
37-
githubToken: ${{ secrets.GITHUB_TOKEN }}
37+
githubToken: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }}
3838
# For non-Pull Requests, run in compressOnly mode and we'll PR after.
3939
compressOnly: ${{ github.event_name != 'pull_request' }}
4040
- name: Create Pull Request

‎.github/workflows/pr-add-label.yml‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ name: pr-add-label
22

33
on:
44
pull_request:
5-
types:
6-
- opened
7-
- synchronize
5+
types: [opened]
6+
7+
permissions:
8+
contents: read
9+
810
jobs:
911
add-label:
12+
permissions:
13+
contents: read
14+
pull-requests: write
1015
runs-on: ubuntu-latest
1116
steps:
1217
- name: Check PR number
@@ -16,7 +21,7 @@ jobs:
1621
- name: Run add-label Action
1722
uses: thinkasany/pr-label-action@master
1823
with:
19-
github_token: ${{ secrets.ACTION_TOKEN }}
24+
github_token: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }}
2025
pr_number: ${{ env.PR_NUMBER }}
2126
organize_name: "doocs"
2227
team_name: "leetcode-algorithm"

‎.github/workflows/pr-checker.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- name: Pull request
1212
uses: actions-cool/maintain-one-comment@v3
1313
with:
14-
token: ${{ secrets.GITHUB_TOKEN }}
14+
token: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }}
1515
body: |
1616
🤭 感谢你的提交,请检查你的改动是否符合以下项目规范。
1717

‎.github/workflows/starcharts.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
steps:
1414
- uses: MaoLongLong/actions-starcharts@main
1515
with:
16-
github_token: ${{ secrets.ACTION_TOKEN }}
16+
github_token: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }}
1717
svg_path: images/starcharts.svg
1818
commit_message: "chore: auto update starcharts"
1919
stars_change: ${{ secrets.STARS_CHANGE }}

‎solution/0500-0599/0542.01 Matrix/README.md‎

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ class Solution:
6666
ans = [[-1] * n for _ in range(m)]
6767
q = deque()
6868
for i, row in enumerate(mat):
69-
for j, v in enumerate(row):
70-
if v == 0:
69+
for j, x in enumerate(row):
70+
if x == 0:
7171
ans[i][j] = 0
7272
q.append((i, j))
73-
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
73+
dirs = (-1, 0, 1, 0, -1)
7474
while q:
7575
i, j = q.popleft()
76-
for a, b in dirs:
76+
for a, b in pairwise(dirs):
7777
x, y = i + a, j + b
7878
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
7979
ans[x][y] = ans[i][j] + 1
@@ -87,30 +87,29 @@ class Solution:
8787

8888
```java
8989
class Solution {
90-
9190
public int[][] updateMatrix(int[][] mat) {
9291
int m = mat.length, n = mat[0].length;
9392
int[][] ans = new int[m][n];
94-
for (int i =0; i < m; ++i) {
95-
Arrays.fill(ans[i], -1);
93+
for (int[] row : ans) {
94+
Arrays.fill(row, -1);
9695
}
97-
Deque<int[]> q = new LinkedList<>();
96+
Deque<int[]> q = new ArrayDeque<>();
9897
for (int i = 0; i < m; ++i) {
9998
for (int j = 0; j < n; ++j) {
10099
if (mat[i][j] == 0) {
101-
ans[i][j] = 0;
102100
q.offer(new int[] {i, j});
101+
ans[i][j] = 0;
103102
}
104103
}
105104
}
106-
int[] dirs = newint[] {-1, 0, 1, 0, -1};
105+
int[] dirs = {-1, 0, 1, 0, -1};
107106
while (!q.isEmpty()) {
108-
int[] t = q.poll();
109-
for (int i = 0; i <4; ++i) {
110-
int x = t[0] + dirs[i];
111-
int y = t[1] + dirs[i + 1];
107+
int[] p = q.poll();
108+
int i = p[0], j = p[1];
109+
for (int k = 0; k <4; ++k) {
110+
int x = i + dirs[k], y = j + dirs[k + 1];
112111
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
113-
ans[x][y] = ans[t[0]][t[1]] + 1;
112+
ans[x][y] = ans[i][j] + 1;
114113
q.offer(new int[] {x, y});
115114
}
116115
}
@@ -244,6 +243,36 @@ func updateMatrix(mat [][]int) [][]int {
244243
}
245244
```
246245

246+
### **TypeScript**
247+
248+
```ts
249+
function updateMatrix(mat: number[][]): number[][] {
250+
const [m, n] = [mat.length, mat[0].length];
251+
const ans: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => -1));
252+
const q: [number, number][] = [];
253+
for (let i = 0; i < m; ++i) {
254+
for (let j = 0; j < n; ++j) {
255+
if (mat[i][j] === 0) {
256+
q.push([i, j]);
257+
ans[i][j] = 0;
258+
}
259+
}
260+
}
261+
const dirs: number[] = [-1, 0, 1, 0, -1];
262+
while (q.length) {
263+
const [i, j] = q.shift()!;
264+
for (let k = 0; k < 4; ++k) {
265+
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
266+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] === -1) {
267+
ans[x][y] = ans[i][j] + 1;
268+
q.push([x, y]);
269+
}
270+
}
271+
}
272+
return ans;
273+
}
274+
```
275+
247276
### **...**
248277

249278
```

‎solution/0500-0599/0542.01 Matrix/README_EN.md‎

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ class Solution:
6060
ans = [[-1] * n for _ in range(m)]
6161
q = deque()
6262
for i, row in enumerate(mat):
63-
for j, v in enumerate(row):
64-
if v == 0:
63+
for j, x in enumerate(row):
64+
if x == 0:
6565
ans[i][j] = 0
6666
q.append((i, j))
67-
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
67+
dirs = (-1, 0, 1, 0, -1)
6868
while q:
6969
i, j = q.popleft()
70-
for a, b in dirs:
70+
for a, b in pairwise(dirs):
7171
x, y = i + a, j + b
7272
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
7373
ans[x][y] = ans[i][j] + 1
@@ -79,30 +79,29 @@ class Solution:
7979

8080
```java
8181
class Solution {
82-
8382
public int[][] updateMatrix(int[][] mat) {
8483
int m = mat.length, n = mat[0].length;
8584
int[][] ans = new int[m][n];
86-
for (int i =0; i < m; ++i) {
87-
Arrays.fill(ans[i], -1);
85+
for (int[] row : ans) {
86+
Arrays.fill(row, -1);
8887
}
89-
Deque<int[]> q = new LinkedList<>();
88+
Deque<int[]> q = new ArrayDeque<>();
9089
for (int i = 0; i < m; ++i) {
9190
for (int j = 0; j < n; ++j) {
9291
if (mat[i][j] == 0) {
93-
ans[i][j] = 0;
9492
q.offer(new int[] {i, j});
93+
ans[i][j] = 0;
9594
}
9695
}
9796
}
98-
int[] dirs = newint[] {-1, 0, 1, 0, -1};
97+
int[] dirs = {-1, 0, 1, 0, -1};
9998
while (!q.isEmpty()) {
100-
int[] t = q.poll();
101-
for (int i = 0; i <4; ++i) {
102-
int x = t[0] + dirs[i];
103-
int y = t[1] + dirs[i + 1];
99+
int[] p = q.poll();
100+
int i = p[0], j = p[1];
101+
for (int k = 0; k <4; ++k) {
102+
int x = i + dirs[k], y = j + dirs[k + 1];
104103
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
105-
ans[x][y] = ans[t[0]][t[1]] + 1;
104+
ans[x][y] = ans[i][j] + 1;
106105
q.offer(new int[] {x, y});
107106
}
108107
}
@@ -236,6 +235,36 @@ func updateMatrix(mat [][]int) [][]int {
236235
}
237236
```
238237

238+
### **TypeScript**
239+
240+
```ts
241+
function updateMatrix(mat: number[][]): number[][] {
242+
const [m, n] = [mat.length, mat[0].length];
243+
const ans: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => -1));
244+
const q: [number, number][] = [];
245+
for (let i = 0; i < m; ++i) {
246+
for (let j = 0; j < n; ++j) {
247+
if (mat[i][j] === 0) {
248+
q.push([i, j]);
249+
ans[i][j] = 0;
250+
}
251+
}
252+
}
253+
const dirs: number[] = [-1, 0, 1, 0, -1];
254+
while (q.length) {
255+
const [i, j] = q.shift()!;
256+
for (let k = 0; k < 4; ++k) {
257+
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
258+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] === -1) {
259+
ans[x][y] = ans[i][j] + 1;
260+
q.push([x, y]);
261+
}
262+
}
263+
}
264+
return ans;
265+
}
266+
```
267+
239268
### **...**
240269

241270
```

‎solution/0500-0599/0542.01 Matrix/Solution.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ class Solution {
22
public int[][] updateMatrix(int[][] mat) {
33
int m = mat.length, n = mat[0].length;
44
int[][] ans = new int[m][n];
5-
for (inti = 0; i < m; ++i) {
6-
Arrays.fill(ans[i], -1);
5+
for (int[] row : ans) {
6+
Arrays.fill(row, -1);
77
}
8-
Deque<int[]> q = new LinkedList<>();
8+
Deque<int[]> q = new ArrayDeque<>();
99
for (int i = 0; i < m; ++i) {
1010
for (int j = 0; j < n; ++j) {
1111
if (mat[i][j] == 0) {
12-
ans[i][j] = 0;
1312
q.offer(new int[] {i, j});
13+
ans[i][j] = 0;
1414
}
1515
}
1616
}
17-
int[] dirs = newint[] {-1, 0, 1, 0, -1};
17+
int[] dirs = {-1, 0, 1, 0, -1};
1818
while (!q.isEmpty()) {
19-
int[] t = q.poll();
20-
for (int i = 0; i < 4; ++i) {
21-
int x = t[0] + dirs[i];
22-
int y = t[1] + dirs[i + 1];
19+
int[] p = q.poll();
20+
int i = p[0], j = p[1];
21+
for (int k = 0; k < 4; ++k) {
22+
int x = i + dirs[k], y = j+ dirs[k + 1];
2323
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
24-
ans[x][y] = ans[t[0]][t[1]] + 1;
24+
ans[x][y] = ans[i][j] + 1;
2525
q.offer(new int[] {x, y});
2626
}
2727
}

‎solution/0500-0599/0542.01 Matrix/Solution.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
33
m, n = len(mat), len(mat[0])
44
ans = [[-1] * n for _ in range(m)]
55
q = deque()
6-
for iin range(m):
7-
for jin range(n):
8-
if mat[i][j] == 0:
6+
for i, rowin enumerate(mat):
7+
for j, xin enumerate(row):
8+
if x == 0:
99
ans[i][j] = 0
1010
q.append((i, j))
11-
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
11+
dirs = (-1, 0, 1, 0, -1)
1212
while q:
1313
i, j = q.popleft()
14-
for a, b in dirs:
14+
for a, b in pairwise(dirs):
1515
x, y = i + a, j + b
1616
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
1717
ans[x][y] = ans[i][j] + 1
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function updateMatrix(mat: number[][]): number[][] {
2+
const [m, n] = [mat.length, mat[0].length];
3+
const ans: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => -1));
4+
const q: [number, number][] = [];
5+
for (let i = 0; i < m; ++i) {
6+
for (let j = 0; j < n; ++j) {
7+
if (mat[i][j] === 0) {
8+
q.push([i, j]);
9+
ans[i][j] = 0;
10+
}
11+
}
12+
}
13+
const dirs: number[] = [-1, 0, 1, 0, -1];
14+
while (q.length) {
15+
const [i, j] = q.shift()!;
16+
for (let k = 0; k < 4; ++k) {
17+
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
18+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] === -1) {
19+
ans[x][y] = ans[i][j] + 1;
20+
q.push([x, y]);
21+
}
22+
}
23+
}
24+
return ans;
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /