1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
image-smoother.cpp 964 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 7年前 . Update image-smoother.cpp
// Time: O(m * n)
// Space: O(1)
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
const auto& m = M.size(), &n = M[0].size();
vector<vector<int>> result(M);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
result[i][j] = getGray(M, i, j);
}
}
return result;
}
private:
int getGray(const vector<vector<int>>& M, int i, int j) {
const auto& m = M.size(), &n = M[0].size();
double total = 0.0;
int count = 0;
for (int r = -1; r < 2; ++r) {
for (int c = -1; c < 2; ++c) {
const auto& ii = i + r;
const auto& jj = j + c;
if (0 <= ii && ii < m && 0 <= jj && jj < n) {
total += M[ii][jj];
++count;
}
}
}
return static_cast<int>(total / count);
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助