Skip to content

Commit 282001a

Browse files
authoredMay 23, 2024
refactor: update typescript solution to lc problem: No.200 (doocs#2884)
1 parent ba88122 commit 282001a

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed
 

‎solution/0200-0299/0200.Number of Islands/README.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,19 @@ function numIslands(grid: string[][]): number {
208208
const m = grid.length;
209209
const n = grid[0].length;
210210
let ans = 0;
211-
function dfs(i, j) {
212-
grid[i][j] = '0';
213-
const dirs = [-1, 0, 1, 0, -1];
214-
for (let k = 0; k < 4; ++k) {
215-
const x = i + dirs[k];
216-
const y = j + dirs[k + 1];
217-
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1') {
218-
dfs(x, y);
219-
}
211+
const dfs = (i: number, j: number) => {
212+
if (grid[i]?.[j] !== '1') {
213+
return;
220214
}
221-
}
215+
grid[i][j] = '0';
216+
dfs(i + 1, j);
217+
dfs(i - 1, j);
218+
dfs(i, j + 1);
219+
dfs(i, j - 1);
220+
};
222221
for (let i = 0; i < m; ++i) {
223222
for (let j = 0; j < n; ++j) {
224-
if (grid[i][j] == '1') {
223+
if (grid[i][j] === '1') {
225224
dfs(i, j);
226225
++ans;
227226
}

‎solution/0200-0299/0200.Number of Islands/README_EN.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,19 @@ function numIslands(grid: string[][]): number {
198198
const m = grid.length;
199199
const n = grid[0].length;
200200
let ans = 0;
201-
function dfs(i, j) {
202-
grid[i][j] = '0';
203-
const dirs = [-1, 0, 1, 0, -1];
204-
for (let k = 0; k < 4; ++k) {
205-
const x = i + dirs[k];
206-
const y = j + dirs[k + 1];
207-
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1') {
208-
dfs(x, y);
209-
}
201+
const dfs = (i: number, j: number) => {
202+
if (grid[i]?.[j] !== '1') {
203+
return;
210204
}
211-
}
205+
grid[i][j] = '0';
206+
dfs(i + 1, j);
207+
dfs(i - 1, j);
208+
dfs(i, j + 1);
209+
dfs(i, j - 1);
210+
};
212211
for (let i = 0; i < m; ++i) {
213212
for (let j = 0; j < n; ++j) {
214-
if (grid[i][j] == '1') {
213+
if (grid[i][j] === '1') {
215214
dfs(i, j);
216215
++ans;
217216
}

‎solution/0200-0299/0200.Number of Islands/Solution.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ function numIslands(grid: string[][]): number {
22
const m = grid.length;
33
const n = grid[0].length;
44
let ans = 0;
5-
function dfs(i, j) {
6-
grid[i][j] = '0';
7-
const dirs = [-1, 0, 1, 0, -1];
8-
for (let k = 0; k < 4; ++k) {
9-
const x = i + dirs[k];
10-
const y = j + dirs[k + 1];
11-
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1') {
12-
dfs(x, y);
13-
}
5+
const dfs = (i: number, j: number) => {
6+
if (grid[i]?.[j] !== '1') {
7+
return;
148
}
15-
}
9+
grid[i][j] = '0';
10+
dfs(i + 1, j);
11+
dfs(i - 1, j);
12+
dfs(i, j + 1);
13+
dfs(i, j - 1);
14+
};
1615
for (let i = 0; i < m; ++i) {
1716
for (let j = 0; j < n; ++j) {
18-
if (grid[i][j] == '1') {
17+
if (grid[i][j] === '1') {
1918
dfs(i, j);
2019
++ans;
2120
}

0 commit comments

Comments
 (0)