Skip to content

Commit af28a40

Browse files
committed
refactor: update typescript solution to lc problem: No.200
1 parent 468d402 commit af28a40

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,17 @@ 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) {
211+
212+
const dfs = (i: number, j: number) => {
213+
if (grid[i]?.[j] !== '1') return;
214+
212215
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-
}
220-
}
221-
}
216+
dfs(i + 1, j);
217+
dfs(i - 1, j);
218+
dfs(i, j + 1);
219+
dfs(i, j - 1);
220+
};
221+
222222
for (let i = 0; i < m; ++i) {
223223
for (let j = 0; j < n; ++j) {
224224
if (grid[i][j] == '1') {
@@ -227,6 +227,7 @@ function numIslands(grid: string[][]): number {
227227
}
228228
}
229229
}
230+
230231
return ans;
231232
}
232233
```

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,17 @@ 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) {
201+
202+
const dfs = (i: number, j: number) => {
203+
if (grid[i]?.[j] !== '1') return;
204+
202205
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-
}
210-
}
211-
}
206+
dfs(i + 1, j);
207+
dfs(i - 1, j);
208+
dfs(i, j + 1);
209+
dfs(i, j - 1);
210+
};
211+
212212
for (let i = 0; i < m; ++i) {
213213
for (let j = 0; j < n; ++j) {
214214
if (grid[i][j] == '1') {
@@ -217,6 +217,7 @@ function numIslands(grid: string[][]): number {
217217
}
218218
}
219219
}
220+
220221
return ans;
221222
}
222223
```

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ 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) {
5+
6+
const dfs = (i: number, j: number) => {
7+
if (grid[i]?.[j] !== '1') return;
8+
69
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-
}
14-
}
15-
}
10+
dfs(i + 1, j);
11+
dfs(i - 1, j);
12+
dfs(i, j + 1);
13+
dfs(i, j - 1);
14+
};
15+
1616
for (let i = 0; i < m; ++i) {
1717
for (let j = 0; j < n; ++j) {
1818
if (grid[i][j] == '1') {
@@ -21,5 +21,6 @@ function numIslands(grid: string[][]): number {
2121
}
2222
}
2323
}
24+
2425
return ans;
2526
}

0 commit comments

Comments
 (0)