-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdf787b
commit ac72836
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Solution { | ||
public: | ||
vector<vector<int>> imageSmoother(vector<vector<int>>& img) { | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(0); | ||
cout.tie(0); | ||
// code | ||
int m = img.size(), n = img[0].size(); | ||
vector<vector<int>> result(m, vector<int>(n)); | ||
|
||
for (int r = 0; r < m; r++) | ||
{ | ||
for (int c = 0; c < n; c++) | ||
{ | ||
int sum = 0, count = 0; | ||
|
||
// Iterate over the 3x3 neighborhood | ||
for (int dr = -1; dr <= 1; dr++) | ||
{ | ||
for (int dc = -1; dc <= 1; dc++) | ||
{ | ||
int nr = r + dr; | ||
int nc = c + dc; | ||
|
||
// Check if the neighbor is within bounds | ||
if ((nr >= 0) && (nr < m) && (nc >= 0) && (nc < n)) | ||
{ | ||
sum += img[nr][nc]; | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
// Calculate the average and update the result matrix | ||
result[r][c] = (sum / count); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
}; |