deno.land / x / masx200_leetcode_test@10.6.5 / flood-fill / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export default function floodFill( image: number[][], sr: number, sc: number, newColor: number,): number[][] { if (newColor === image[sr][sc]) return image; const currColor = image[sr][sc]; dfs(image, sr, sc, currColor, newColor); return image;}const directions: Array<[number, number]> = [ [0, 1], [1, 0], [0, -1], [-1, 0],];function dfs( image: number[][], sr: number, sc: number, oldColor: number, newColor: number,) { const row = image.length; const col = image[0].length; if (sr >= row || sr < 0 || sc < 0 || sc >= col) { return; } if (image[sr][sc] === oldColor) { image[sr][sc] = newColor;
for (const [i, j] of directions) { const x = sr + i; const y = sc + j; if (x >= 0 && x < row && y >= 0 && y < col) { dfs(image, x, y, oldColor, newColor); } } }}
masx200_leetcode_test

Version Info

Tagged at
a year ago