Skip to content

Commit bd46fe3

Browse files
committed
feat: add solutions to lc problem: No.0661
No.0661.Image Smoother
1 parent 8c28244 commit bd46fe3

File tree

6 files changed

+266
-4
lines changed

6 files changed

+266
-4
lines changed

solution/0600-0699/0661.Image Smoother/README.md

+92-2
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,50 @@
6464
<!-- 这里可写当前语言的特殊实现逻辑 -->
6565

6666
```python
67-
67+
class Solution:
68+
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
69+
m, n = len(img), len(img[0])
70+
ans = [[0] * n for _ in range(m)]
71+
for i in range(m):
72+
for j in range(n):
73+
s = cnt = 0
74+
for x in range(i - 1, i + 2):
75+
for y in range(j - 1, j + 2):
76+
if 0 <= x < m and 0 <= y < n:
77+
cnt += 1
78+
s += img[x][y]
79+
ans[i][j] = s // cnt
80+
return ans
6881
```
6982

7083
### **Java**
7184

7285
<!-- 这里可写当前语言的特殊实现逻辑 -->
7386

7487
```java
75-
88+
class Solution {
89+
public int[][] imageSmoother(int[][] img) {
90+
int m = img.length;
91+
int n = img[0].length;
92+
int[][] ans = new int[m][n];
93+
for (int i = 0; i < m; ++i) {
94+
for (int j = 0; j < n; ++j) {
95+
int s = 0;
96+
int cnt = 0;
97+
for (int x = i - 1; x <= i + 1; ++x) {
98+
for (int y = j - 1; y <= j + 1; ++y) {
99+
if (x >= 0 && x < m && y >= 0 && y < n) {
100+
++cnt;
101+
s += img[x][y];
102+
}
103+
}
104+
}
105+
ans[i][j] = s / cnt;
106+
}
107+
}
108+
return ans;
109+
}
110+
}
76111
```
77112

78113
### **TypeScript**
@@ -154,6 +189,61 @@ impl Solution {
154189
}
155190
```
156191

192+
### **C++**
193+
194+
```cpp
195+
class Solution {
196+
public:
197+
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
198+
int m = img.size(), n = img[0].size();
199+
vector<vector<int>> ans(m, vector<int>(n));
200+
for (int i = 0; i < m; ++i)
201+
{
202+
for (int j = 0; j < n; ++j)
203+
{
204+
int s = 0, cnt = 0;
205+
for (int x = i - 1; x <= i + 1; ++x)
206+
{
207+
for (int y = j - 1; y <= j + 1; ++y)
208+
{
209+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
210+
++cnt;
211+
s += img[x][y];
212+
}
213+
}
214+
ans[i][j] = s / cnt;
215+
}
216+
}
217+
return ans;
218+
}
219+
};
220+
```
221+
222+
### **Go**
223+
224+
```go
225+
func imageSmoother(img [][]int) [][]int {
226+
m, n := len(img), len(img[0])
227+
ans := make([][]int, m)
228+
for i, row := range img {
229+
ans[i] = make([]int, n)
230+
for j := range row {
231+
s, cnt := 0, 0
232+
for x := i - 1; x <= i+1; x++ {
233+
for y := j - 1; y <= j+1; y++ {
234+
if x >= 0 && x < m && y >= 0 && y < n {
235+
cnt++
236+
s += img[x][y]
237+
}
238+
}
239+
}
240+
ans[i][j] = s / cnt
241+
}
242+
}
243+
return ans
244+
}
245+
```
246+
157247
### **...**
158248

159249
```

solution/0600-0699/0661.Image Smoother/README_EN.md

+92-2
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,48 @@ For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.8
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
53+
m, n = len(img), len(img[0])
54+
ans = [[0] * n for _ in range(m)]
55+
for i in range(m):
56+
for j in range(n):
57+
s = cnt = 0
58+
for x in range(i - 1, i + 2):
59+
for y in range(j - 1, j + 2):
60+
if 0 <= x < m and 0 <= y < n:
61+
cnt += 1
62+
s += img[x][y]
63+
ans[i][j] = s // cnt
64+
return ans
5265
```
5366

5467
### **Java**
5568

5669
```java
57-
70+
class Solution {
71+
public int[][] imageSmoother(int[][] img) {
72+
int m = img.length;
73+
int n = img[0].length;
74+
int[][] ans = new int[m][n];
75+
for (int i = 0; i < m; ++i) {
76+
for (int j = 0; j < n; ++j) {
77+
int s = 0;
78+
int cnt = 0;
79+
for (int x = i - 1; x <= i + 1; ++x) {
80+
for (int y = j - 1; y <= j + 1; ++y) {
81+
if (x >= 0 && x < m && y >= 0 && y < n) {
82+
++cnt;
83+
s += img[x][y];
84+
}
85+
}
86+
}
87+
ans[i][j] = s / cnt;
88+
}
89+
}
90+
return ans;
91+
}
92+
}
5893
```
5994

6095
### **TypeScript**
@@ -136,6 +171,61 @@ impl Solution {
136171
}
137172
```
138173

174+
### **C++**
175+
176+
```cpp
177+
class Solution {
178+
public:
179+
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
180+
int m = img.size(), n = img[0].size();
181+
vector<vector<int>> ans(m, vector<int>(n));
182+
for (int i = 0; i < m; ++i)
183+
{
184+
for (int j = 0; j < n; ++j)
185+
{
186+
int s = 0, cnt = 0;
187+
for (int x = i - 1; x <= i + 1; ++x)
188+
{
189+
for (int y = j - 1; y <= j + 1; ++y)
190+
{
191+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
192+
++cnt;
193+
s += img[x][y];
194+
}
195+
}
196+
ans[i][j] = s / cnt;
197+
}
198+
}
199+
return ans;
200+
}
201+
};
202+
```
203+
204+
### **Go**
205+
206+
```go
207+
func imageSmoother(img [][]int) [][]int {
208+
m, n := len(img), len(img[0])
209+
ans := make([][]int, m)
210+
for i, row := range img {
211+
ans[i] = make([]int, n)
212+
for j := range row {
213+
s, cnt := 0, 0
214+
for x := i - 1; x <= i+1; x++ {
215+
for y := j - 1; y <= j+1; y++ {
216+
if x >= 0 && x < m && y >= 0 && y < n {
217+
cnt++
218+
s += img[x][y]
219+
}
220+
}
221+
}
222+
ans[i][j] = s / cnt
223+
}
224+
}
225+
return ans
226+
}
227+
```
228+
139229
### **...**
140230

141231
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
4+
int m = img.size(), n = img[0].size();
5+
vector<vector<int>> ans(m, vector<int>(n));
6+
for (int i = 0; i < m; ++i)
7+
{
8+
for (int j = 0; j < n; ++j)
9+
{
10+
int s = 0, cnt = 0;
11+
for (int x = i - 1; x <= i + 1; ++x)
12+
{
13+
for (int y = j - 1; y <= j + 1; ++y)
14+
{
15+
if (x < 0 || x >= m || y < 0 || y >= n) continue;
16+
++cnt;
17+
s += img[x][y];
18+
}
19+
}
20+
ans[i][j] = s / cnt;
21+
}
22+
}
23+
return ans;
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func imageSmoother(img [][]int) [][]int {
2+
m, n := len(img), len(img[0])
3+
ans := make([][]int, m)
4+
for i, row := range img {
5+
ans[i] = make([]int, n)
6+
for j := range row {
7+
s, cnt := 0, 0
8+
for x := i - 1; x <= i+1; x++ {
9+
for y := j - 1; y <= j+1; y++ {
10+
if x >= 0 && x < m && y >= 0 && y < n {
11+
cnt++
12+
s += img[x][y]
13+
}
14+
}
15+
}
16+
ans[i][j] = s / cnt
17+
}
18+
}
19+
return ans
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int[][] imageSmoother(int[][] img) {
3+
int m = img.length;
4+
int n = img[0].length;
5+
int[][] ans = new int[m][n];
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
int s = 0;
9+
int cnt = 0;
10+
for (int x = i - 1; x <= i + 1; ++x) {
11+
for (int y = j - 1; y <= j + 1; ++y) {
12+
if (x >= 0 && x < m && y >= 0 && y < n) {
13+
++cnt;
14+
s += img[x][y];
15+
}
16+
}
17+
}
18+
ans[i][j] = s / cnt;
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
3+
m, n = len(img), len(img[0])
4+
ans = [[0] * n for _ in range(m)]
5+
for i in range(m):
6+
for j in range(n):
7+
s = cnt = 0
8+
for x in range(i - 1, i + 2):
9+
for y in range(j - 1, j + 2):
10+
if 0 <= x < m and 0 <= y < n:
11+
cnt += 1
12+
s += img[x][y]
13+
ans[i][j] = s // cnt
14+
return ans

0 commit comments

Comments
 (0)