Skip to content

Commit 200fde9

Browse files
authored
Merge pull request #627 from LetMeFly666/661
添加问题“661.图片平滑器”的代码和题解
2 parents 190a271 + be03589 commit 200fde9

File tree

6 files changed

+276
-0
lines changed

6 files changed

+276
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-18 21:03:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-18 21:08:57
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
14+
vector<vector<int>> ans(img.size(), vector<int>(img[0].size()));
15+
for (int i = 0; i < img.size(); i++) {
16+
for (int j = 0; j < img[0].size(); j++) {
17+
int cnt = 0, s = 0;
18+
for (int x = -1; x <= 1; x++) {
19+
for (int y = -1; y <= 1; y++) {
20+
int nx = i + x, ny = j + y;
21+
if (0 <= nx && nx < img.size() && 0 <= ny && ny < img[0].size()) {
22+
s += img[nx][ny];
23+
cnt++;
24+
}
25+
}
26+
}
27+
ans[i][j] = s / cnt;
28+
}
29+
}
30+
return ans;
31+
}
32+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-18 21:23:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-18 21:35:14
6+
*/
7+
package main
8+
9+
func imageSmoother(img [][]int) (ans [][]int) {
10+
ans = make([][]int, len(img))
11+
for i := range ans {
12+
ans[i] = make([]int, len(img[0]))
13+
for j := range ans[i] {
14+
cnt, s := 0, 0
15+
for _, row := range img[max(0, i - 1):min(len(img), i + 2)] {
16+
for _, val := range row[max(0, j - 1):min(len(img[0]), j + 2)] {
17+
cnt++
18+
s += val
19+
}
20+
}
21+
ans[i][j] = s / cnt
22+
}
23+
}
24+
return ans
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-18 21:13:44
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-18 21:36:01
6+
*/
7+
class Solution {
8+
public int[][] imageSmoother(int[][] img) {
9+
int[][] ans = new int[img.length][img[0].length];
10+
for (int i = 0; i < img.length; i++) {
11+
for (int j = 0; j < img[0].length; j++) {
12+
int cnt = 0, s = 0;
13+
for (int dx = -1; dx <= 1; dx++) {
14+
for (int dy = -1; dy <= 1; dy++) {
15+
int x = i + dx, y = j + dy;
16+
if (0 <= x && x < img.length && 0 <= y && y < img[0].length) {
17+
cnt++;
18+
s += img[x][y];
19+
}
20+
}
21+
}
22+
ans[i][j] = s / cnt;
23+
}
24+
}
25+
return ans;
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-18 21:10:17
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-18 21:12:52
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
11+
ans = [[0 for _ in range(len(img[0]))] for _ in range(len(img))]
12+
for i in range(len(img)):
13+
for j in range(len(img[0])):
14+
cnt, s = 0, 0
15+
for dx in range(-1, 2):
16+
for dy in range(-1, 2):
17+
x, y = i + dx, j + dy
18+
if 0 <= x < len(img) and 0 <= y < len(img[0]):
19+
cnt += 1
20+
s += img[x][y]
21+
ans[i][j] = s // cnt
22+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@
233233
|0646.最长数对链|中等|<a href="https://leetcode.cn/problems/maximum-length-of-pair-chain/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/03/LeetCode%200646.%E6%9C%80%E9%95%BF%E6%95%B0%E5%AF%B9%E9%93%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126682589" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-length-of-pair-chain/solution/letmefly-646zui-chang-shu-dui-lian-by-ti-bia2/" target="_blank">LeetCode题解</a>|
234234
|0654.最大二叉树|中等|<a href="https://leetcode.cn/problems/maximum-binary-tree/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/20/LeetCode%200654.%E6%9C%80%E5%A4%A7%E4%BA%8C%E5%8F%89%E6%A0%91/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126443463" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-binary-tree/solution/letmefly-shi-pin-yan-shi-654zui-da-er-ch-2fcl/" target="_blank">LeetCode题解</a>|
235235
|0655.输出二叉树|中等|<a href="https://leetcode.cn/problems/print-binary-tree/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/22/LeetCode%200655.%E8%BE%93%E5%87%BA%E4%BA%8C%E5%8F%89%E6%A0%91/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126461060" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/print-binary-tree/solution/letmefly-655shu-chu-er-cha-shu-by-tisfy-5xj2/" target="_blank">LeetCode题解</a>|
236+
|0661.图片平滑器|简单|<a href="https://leetcode.cn/problems/image-smoother/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/18/LeetCode%200661.%E5%9B%BE%E7%89%87%E5%B9%B3%E6%BB%91%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143867615" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/image-smoother/solutions/2992934/letmefly-661tu-pian-ping-hua-qi-mo-ni-by-g8s7/" target="_blank">LeetCode题解</a>|
236237
|0662.二叉树最大宽度|中等|<a href="https://leetcode.cn/problems/maximum-width-of-binary-tree/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/27/LeetCode%200662.%E4%BA%8C%E5%8F%89%E6%A0%91%E6%9C%80%E5%A4%A7%E5%AE%BD%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126558271" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-width-of-binary-tree/solution/letmefly-662er-cha-shu-zui-da-kuan-du-by-yny2/" target="_blank">LeetCode题解</a>|
237238
|0667.优美的排列II|中等|<a href="https://leetcode.cn/problems/beautiful-arrangement-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/08/LeetCode%200667.%E4%BC%98%E7%BE%8E%E7%9A%84%E6%8E%92%E5%88%97II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126759880" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/beautiful-arrangement-ii/solution/letmefly-667you-mei-de-pai-lie-ii-by-tis-2dzp/" target="_blank">LeetCode题解</a>|
238239
|0670.最大交换|中等|<a href="https://leetcode.cn/problems/maximum-swap/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/13/LeetCode%200670.%E6%9C%80%E5%A4%A7%E4%BA%A4%E6%8D%A2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126826280" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-swap/solution/letmefly-670zui-da-jiao-huan-by-tisfy-0gg5/" target="_blank">LeetCode题解</a>|
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
title: 661.图片平滑器
3+
date: 2024-11-18 21:36:49
4+
tags: [题解, LeetCode, 简单, 数组, 矩阵, 模拟]
5+
---
6+
7+
# 【LetMeFly】661.图片平滑器:模拟
8+
9+
力扣题目链接:[https://leetcode.cn/problems/image-smoother/](https://leetcode.cn/problems/image-smoother/)
10+
11+
<p><strong>图像平滑器</strong> 是大小为&nbsp;<code>3 x 3</code> 的过滤器,用于对图像的每个单元格平滑处理,平滑处理后单元格的值为该单元格的平均灰度。</p>
12+
13+
<p>每个单元格的<strong>&nbsp; 平均灰度</strong> 定义为:该单元格自身及其周围的 8 个单元格的平均值,结果需向下取整。(即,需要计算蓝色平滑器中 9 个单元格的平均值)。</p>
14+
15+
<p>如果一个单元格周围存在单元格缺失的情况,则计算平均灰度时不考虑缺失的单元格(即,需要计算红色平滑器中 4 个单元格的平均值)。</p>
16+
17+
<p><img src="https://assets.leetcode.com/uploads/2021/05/03/smoother-grid.jpg" style="height: 493px; width: 493px;" /></p>
18+
19+
<p>给你一个表示图像灰度的 <code>m x n</code> 整数矩阵 <code>img</code> ,返回对图像的每个单元格平滑处理后的图像&nbsp;。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>示例 1:</strong></p>
24+
25+
<p><img src="https://assets.leetcode.com/uploads/2021/05/03/smooth-grid.jpg" /></p>
26+
27+
<pre>
28+
<strong>输入:</strong>img = [[1,1,1],[1,0,1],[1,1,1]]
29+
<strong>输出:</strong>[[0, 0, 0],[0, 0, 0], [0, 0, 0]]
30+
<strong>解释:</strong>
31+
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
32+
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
33+
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
34+
</pre>
35+
36+
<p><strong>示例 2:</strong></p>
37+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/smooth2-grid.jpg" />
38+
<pre>
39+
<strong>输入:</strong> img = [[100,200,100],[200,50,200],[100,200,100]]
40+
<strong>输出:</strong> [[137,141,137],[141,138,141],[137,141,137]]
41+
<strong>解释:</strong>
42+
对于点 (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
43+
对于点 (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
44+
对于点 (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>m == img.length</code></li>
53+
<li><code>n == img[i].length</code></li>
54+
<li><code>1 &lt;= m, n &lt;= 200</code></li>
55+
<li><code>0 &lt;= img[i][j] &lt;= 255</code></li>
56+
</ul>
57+
58+
59+
60+
## 解题方法:模拟
61+
62+
依次枚举每个元素,对于某个元素枚举其3x3矩阵(若超出边界则跳过)。按题目要求,对边界问题细心一点并计算即可。
63+
64+
+ 时间复杂度$O(mnC^2)$
65+
+ 空间复杂度$O(1)$:力扣返回值不计入算法空间复杂度
66+
67+
### AC代码
68+
69+
#### C++
70+
71+
```cpp
72+
class Solution {
73+
public:
74+
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
75+
vector<vector<int>> ans(img.size(), vector<int>(img[0].size()));
76+
for (int i = 0; i < img.size(); i++) {
77+
for (int j = 0; j < img[0].size(); j++) {
78+
int cnt = 0, s = 0;
79+
for (int x = -1; x <= 1; x++) {
80+
for (int y = -1; y <= 1; y++) {
81+
int nx = i + x, ny = j + y;
82+
if (0 <= nx && nx < img.size() && 0 <= ny && ny < img[0].size()) {
83+
s += img[nx][ny];
84+
cnt++;
85+
}
86+
}
87+
}
88+
ans[i][j] = s / cnt;
89+
}
90+
}
91+
return ans;
92+
}
93+
};
94+
```
95+
96+
#### Python
97+
98+
```python
99+
from typing import List
100+
101+
class Solution:
102+
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
103+
ans = [[0 for _ in range(len(img[0]))] for _ in range(len(img))]
104+
for i in range(len(img)):
105+
for j in range(len(img[0])):
106+
cnt, s = 0, 0
107+
for dx in range(-1, 2):
108+
for dy in range(-1, 2):
109+
x, y = i + dx, j + dy
110+
if 0 <= x < len(img) and 0 <= y < len(img[0]):
111+
cnt += 1
112+
s += img[x][y]
113+
ans[i][j] = s // cnt
114+
return ans
115+
```
116+
117+
#### Java
118+
119+
```java
120+
class Solution {
121+
public int[][] imageSmoother(int[][] img) {
122+
int[][] ans = new int[img.length][img[0].length];
123+
for (int i = 0; i < img.length; i++) {
124+
for (int j = 0; j < img[0].length; j++) {
125+
int cnt = 0, s = 0;
126+
for (int dx = -1; dx <= 1; dx++) {
127+
for (int dy = -1; dy <= 1; dy++) {
128+
int x = i + dx, y = j + dy;
129+
if (0 <= x && x < img.length && 0 <= y && y < img[0].length) {
130+
cnt++;
131+
s += img[x][y];
132+
}
133+
}
134+
}
135+
ans[i][j] = s / cnt;
136+
}
137+
}
138+
return ans;
139+
}
140+
}
141+
```
142+
143+
#### Go
144+
145+
```go
146+
package main
147+
148+
func imageSmoother(img [][]int) (ans [][]int) {
149+
ans = make([][]int, len(img))
150+
for i := range ans {
151+
ans[i] = make([]int, len(img[0]))
152+
for j := range ans[i] {
153+
cnt, s := 0, 0
154+
for _, row := range img[max(0, i - 1):min(len(img), i + 2)] {
155+
for _, val := range row[max(0, j - 1):min(len(img[0]), j + 2)] {
156+
cnt++
157+
s += val
158+
}
159+
}
160+
ans[i][j] = s / cnt
161+
}
162+
}
163+
return ans
164+
}
165+
```
166+
167+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/11/18/LeetCode%200661.%E5%9B%BE%E7%89%87%E5%B9%B3%E6%BB%91%E5%99%A8/)哦~
168+
>
169+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/143867615](https://letmefly.blog.csdn.net/article/details/143867615)

0 commit comments

Comments
 (0)