LeetCode661 Image Smoother

文章目录
  1. 1. 题目
  2. 2. 思路
  3. 3. 代码

题目

LeetCode661 Image Smoother

思路

按题意模拟即可.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
const int dx[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int n = M.size(), m = M[0].size();
vector<vector<int>> ret(n, vector<int>());
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int sum = 0, cnt = 0;
for (int k = 0; k < 9; ++k) {
int x = i + dx[k], y = j + dy[k];
if (x < 0 || x >= n || y < 0 || y >= m) continue;
sum += M[x][y];
cnt++;
}
ret[i].push_back(sum / cnt);
}
}
return ret;
}
};
分享到 评论