@@ -48,13 +48,48 @@ For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.8
48
48
### ** Python3**
49
49
50
50
``` 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
52
65
```
53
66
54
67
### ** Java**
55
68
56
69
``` 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
+ }
58
93
```
59
94
60
95
### ** TypeScript**
@@ -136,6 +171,61 @@ impl Solution {
136
171
}
137
172
```
138
173
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
+
139
229
### ** ...**
140
230
141
231
```
0 commit comments