-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0661.图片平滑器.java
94 lines (89 loc) · 2.77 KB
/
0661.图片平滑器.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* @lc app=leetcode.cn id=661 lang=java
*
* [661] 图片平滑器
*
* https://leetcode.cn/problems/image-smoother/description/
*
* algorithms
* Easy (64.18%)
* Likes: 204
* Dislikes: 0
* Total Accepted: 56.3K
* Total Submissions: 87.8K
* Testcase Example: '[[1,1,1],[1,0,1],[1,1,1]]'
*
* 图像平滑器 是大小为 3 x 3 的过滤器,用于对图像的每个单元格平滑处理,平滑处理后单元格的值为该单元格的平均灰度。
*
* 每个单元格的 平均灰度 定义为:该单元格自身及其周围的 8 个单元格的平均值,结果需向下取整。(即,需要计算蓝色平滑器中 9 个单元格的平均值)。
*
* 如果一个单元格周围存在单元格缺失的情况,则计算平均灰度时不考虑缺失的单元格(即,需要计算红色平滑器中 4 个单元格的平均值)。
*
*
*
* 给你一个表示图像灰度的 m x n 整数矩阵 img ,返回对图像的每个单元格平滑处理后的图像 。
*
*
*
* 示例 1:
*
*
*
*
* 输入:img = [[1,1,1],[1,0,1],[1,1,1]]
* 输出:[[0, 0, 0],[0, 0, 0], [0, 0, 0]]
* 解释:
* 对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
* 对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
* 对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
*
*
* 示例 2:
*
*
* 输入: img = [[100,200,100],[200,50,200],[100,200,100]]
* 输出: [[137,141,137],[141,138,141],[137,141,137]]
* 解释:
* 对于点 (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) =
* 137
* 对于点 (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) =
* floor(141.666667) = 141
* 对于点 (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889)
* = 138
*
*
*
*
* 提示:
*
*
* m == img.length
* n == img[i].length
* 1 <= m, n <= 200
* 0 <= img[i][j] <= 255
*
*
*/
// @lc code=start
class Solution {
public int[][] imageSmoother(int[][] img) {
int m = img.length, n = img[0].length;
int[][] sum = new int[m + 10][n + 10];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + img[i - 1][j - 1];
}
int[][] matrix = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int u = Math.max(0, i - 1), l = Math.max(0, j - 1);
int b = Math.min(m - 1, i + 1), r = Math.min(n - 1, j + 1);
int count = (b - u + 1) * (r - l + 1);
int total = sum[b + 1][r + 1] - sum[u][r + 1] - sum[b + 1][l] + sum[u][l];
matrix[i][j] = total / count;
}
}
return matrix;
}
}
// @lc code=end