Skip to content

Commit 73904d1

Browse files
committed
661
1 parent d0a7ac5 commit 73904d1

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

661.Image-Smoother/main.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[link] https://leetcode.com/problems/image-smoother/description/
2+
3+
[661]
4+
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.
5+
Example 1:
6+
Input:
7+
[[1,1,1],
8+
[1,0,1],
9+
[1,1,1]]
10+
Output:
11+
[[0, 0, 0],
12+
[0, 0, 0],
13+
[0, 0, 0]]
14+
Explanation:
15+
16+
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
17+
18+
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
19+
20+
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
21+
22+
Note:
23+
24+
The value in the given matrix is in the range of [0, 255].
25+
26+
The length and width of the given matrix are in the range of [1, 150].
27+
28+
[solution]
29+
class Solution {
30+
public:
31+
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
32+
vector<vector<int>> ans(M);
33+
int sum = 0,cnt=0;
34+
int row = M.size();
35+
int col= M[0].size();
36+
for( int i = 0; i < row; ++i)
37+
{
38+
for( int j = 0; j < col; ++j)
39+
{
40+
sum =cnt = 0;
41+
for( int ii= i-1; ii<= i+1; ++ii )
42+
{
43+
for( int jj= j-1; jj <= j+1; ++jj )
44+
{
45+
if( ii< 0 || jj < 0 || ii >= row|| jj >= col) continue;
46+
sum += M[ii][jj];
47+
cnt++;
48+
}
49+
}
50+
ans[i][j] = sum / cnt;
51+
}
52+
}
53+
return ans;
54+
}
55+
};
56+
57+
58+

0 commit comments

Comments
 (0)