Skip to content

Commit f26ccce

Browse files
committed
https://leetcode.cn/problems/image-smoother/
1 parent 7ab5066 commit f26ccce

File tree

5 files changed

+61
-35
lines changed

5 files changed

+61
-35
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/image-smoother/
14+
1315
https://leetcode.cn/problems/crawler-log-folder/
1416

1517
https://leetcode.cn/problems/unique-paths-ii

crawler-log-folder/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export default function minOperations(logs: string[]): number {
2-
return (
3-
Array.from(new URL(logs.join(""), "h://a.b").pathname.matchAll(/\//g))
4-
.length - 1
5-
);
6-
}
1+
export default function minOperations(logs: string[]): number {
2+
return (
3+
Array.from(new URL(logs.join(""), "h://a.b").pathname.matchAll(/\//g))
4+
.length - 1
5+
);
6+
}

image-smoother/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function imageSmoother(img: number[][]): number[][] {
2+
const dirs = [
3+
[-1, -1],
4+
[-1, 0],
5+
[-1, 1],
6+
[0, -1],
7+
[0, 1],
8+
[1, -1],
9+
[1, 0],
10+
[1, 1],
11+
];
12+
return img.map((a, i) =>
13+
a.map((v, j) => {
14+
const all = [
15+
v,
16+
...dirs
17+
.map(([r, c]) => img[i + r]?.[j + c])
18+
.filter(Number.isInteger),
19+
] as number[];
20+
return Math.floor(all.reduce((a, v) => a + v) / all.length);
21+
})
22+
);
23+
}
24+
export default imageSmoother;

rectangle-overlap/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export default function isRectangleOverlap(
2-
rec1: number[],
3-
rec2: number[]
4-
): boolean {
5-
return (
6-
Math.min(rec1[2], rec2[2]) > Math.max(rec1[0], rec2[0]) &&
7-
Math.min(rec1[3], rec2[3]) > Math.max(rec1[1], rec2[1])
8-
);
9-
}
1+
export default function isRectangleOverlap(
2+
rec1: number[],
3+
rec2: number[],
4+
): boolean {
5+
return (
6+
Math.min(rec1[2], rec2[2]) > Math.max(rec1[0], rec2[0]) &&
7+
Math.min(rec1[3], rec2[3]) > Math.max(rec1[1], rec2[1])
8+
);
9+
}

unique-paths-ii/index.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
2-
if (obstacleGrid[0][0] === 1) return 0;
3-
const m = obstacleGrid.length,
4-
n = obstacleGrid[0].length;
5-
if (obstacleGrid[m - 1][n - 1] === 1) return 0;
6-
7-
const f: number[] = Array(n).fill(0);
8-
f[0] = 1;
9-
for (let i = 0; i < m; i++) {
10-
for (let j = 0; j < n; j++) {
11-
if (obstacleGrid[i][j] === 1) {
12-
f[j] = 0;
13-
} else if (j >= 1 && obstacleGrid[i][j - 1] === 0) {
14-
f[j] = f[j] + f[j - 1];
15-
}
16-
}
17-
}
18-
return f[f.length - 1];
19-
}
20-
export default uniquePathsWithObstacles;
1+
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
2+
if (obstacleGrid[0][0] === 1) return 0;
3+
const m = obstacleGrid.length,
4+
n = obstacleGrid[0].length;
5+
if (obstacleGrid[m - 1][n - 1] === 1) return 0;
6+
7+
const f: number[] = Array(n).fill(0);
8+
f[0] = 1;
9+
for (let i = 0; i < m; i++) {
10+
for (let j = 0; j < n; j++) {
11+
if (obstacleGrid[i][j] === 1) {
12+
f[j] = 0;
13+
} else if (j >= 1 && obstacleGrid[i][j - 1] === 0) {
14+
f[j] = f[j] + f[j - 1];
15+
}
16+
}
17+
}
18+
return f[f.length - 1];
19+
}
20+
export default uniquePathsWithObstacles;

0 commit comments

Comments
 (0)