图像平滑器 是大小为 3 x 3 的过滤器,用于对图像的每个单元格平滑处理,平滑处理后单元格的值为该单元格的平均灰度。
每个单元格的 平均灰度 定义为:该单元格自身及其周围的 8 个单元格的平均值,结果需向下取整。(即,需要计算蓝色平滑器中 9 个单元格的平均值)。
如果一个单元格周围存在单元格缺失的情况,则计算平均灰度时不考虑缺失的单元格(即,需要计算红色平滑器中 4 个单元格的平均值)。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/image-smoother
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
1.模拟解法
我的最终代码:

class Solution { private: const int dir[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; public: vector<vector<int>> imageSmoother(vector<vector<int>>& img) { int n=img.size();int m=img[0].size(); vector<vector<int>> ans(n,vector<int>(m)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { int val=img[i][j]; int num=9; for(int d=0;d<8;d++) { if(i+dir[d][0]>=0&&i+dir[d][0]<n&&j+dir[d][1]>=0&&j+dir[d][1]<m) { val+=img[i+dir[d][0]][j+dir[d][1]]; } else num--; } ans[i][j]=val/num; } } return ans; } };
我的错误代码,及原因分析:

class Solution { private: const int dir[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; public: vector<vector<int>> imageSmoother(vector<vector<int>>& img) { int n=img.size();int m=img[0].size(); vector<vector<int>> ans(n,vector<int>(m)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { int val=img[i][j]; int n=9; for(int d=0;d<8;d++) { if(i+dir[d][0]>=0&&i+dir[d][0]<n&&j+dir[d][1]>=0&&j+dir[d][1]<m) { val+=img[i+dir[d][0]][j+dir[d][1]]; } else {n--;} } ans[i][j]=val/n; } } return ans; } }; //在这里有错误,虽然小而傻但是要重视。 //错误间接因素1.习惯性用n,m定义高,宽, //错误间接因素2.习惯性用n 定义当前计数; //导致错误:n变量被定义两次,但自己很难发现,部分编译器甚至不报错。
Leetcode官方解答代码:

class Solution { public: vector<vector<int>> imageSmoother(vector<vector<int>>& img) { int m = img.size(), n = img[0].size(); vector<vector<int>> ret(m, vector<int>(n)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int num = 0, sum = 0; for (int x = i - 1; x <= i + 1; x++) { for (int y = j - 1; y <= j + 1; y++) { if (x >= 0 && x < m && y >= 0 && y < n) { num++; sum += img[x][y]; } } } ret[i][j] = sum / num; } } return ret; } }; 作者:LeetCode-Solution 链接:https://leetcode-cn.com/problems/image-smoother/solution/tu-pian-ping-hua-qi-by-leetcode-solution-9oi5/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- 时间复杂度:O(m * n*R) R为9个单位方块固定为9
- 空间复杂度:O(m * n) 复杂度一定要记得分析
总结:定义变量谨防重复定义。
2.前缀和解法
来自 【Leetcode大佬:宫水三叶】
在朴素解法中,对于每个 ans[i][j]ans[i][j] 我们都不可避免的遍历 88 联通方向,而利用「前缀和」我们可以对该操作进行优化。
对于某个 ans[i][j]ans[i][j] 而言,我们可以直接计算出其所在 item 的左上角 (a, b) = (i - 1, j - 1)(a,b)=(i−1,j−1) 以及其右下角 (c, d) = (i + 1, j + 1)(c,d)=(i+1,j+1),同时为了防止超出原矩阵,我们需要将 (a, b)(a,b) 与 (c, d)(c,d) 对边界分别取 max 和 min。
当有了合法的 (a, b)(a,b) 和 (c, d)(c,d) 后,我们可以直接计算出 item 的单元格数量(所包含的行列乘积)及 item 的单元格之和(前缀和查询),从而算得 ans[i][j]ans[i][j]。
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[][] ans = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) {
//我的注解:限制a和c的取值,以防越界。 int a = Math.max(0, i - 1), b = Math.max(0, j - 1); int c = Math.min(m - 1, i + 1), d = Math.min(n - 1, j + 1);
//我的注解:获取当前区块有几个格子 记为 cnt int cnt = (c - a + 1) * (d - b + 1);
//我的注解:获取当前整个区块的值 int tot = sum[c + 1][d + 1] - sum[a][d + 1] - sum[c + 1][b] + sum[a][b]; ans[i][j] = tot / cnt; } } return ans; } }
- 时间复杂度:O(m * n)
- 空间复杂度:O(m * n) 复杂度一定要记得分析
作者:AC_OIer
链接:https://leetcode-cn.com/problems/image-smoother/solution/by-ac_oier-nn3v/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异