From 9f41f5480acee426ad5b5f0131e148e8284aa32b Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Thu, 24 Mar 2022 20:18:40 +0800 Subject: [PATCH] add: 220324-CN [cpp] --- cpp/2203/220324-CN.cpp | 34 ++++++++++++++++++++++++++++++++++ cpp/2203/CMakeLists.txt | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 cpp/2203/220324-CN.cpp diff --git a/cpp/2203/220324-CN.cpp b/cpp/2203/220324-CN.cpp new file mode 100644 index 0000000..20f818a --- /dev/null +++ b/cpp/2203/220324-CN.cpp @@ -0,0 +1,34 @@ +#include + +/** + * 661. Image Smoother + * An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother). + * Given an m x n integer matrix img representing the grayscale of an image, return the image after applying the smoother on each cell of it. + */ + +class Solution { +public: + static std::vector> imageSmoother(const std::vector>& img) { + auto m = img.size(), n = img.front().size(); + int* tmp = new int[n]; + std::vector> ret; + ret.reserve(m); + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int cnt = 1, total = img[i][j]; + for (int k = -1; k <= 1; ++k) { + for (int l = -1; l <= 1; ++l) { + if (i + k >= 0 && i + k < m && j + l >= 0 && j + l < n && (k || l)) { + total += img[i + k][j + l]; + ++cnt; + } + } + } + tmp[j] = total / cnt; + } + ret.emplace_back(tmp, tmp + n); + } + delete[] tmp; + return ret; + } +}; diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index d2bdcc1..474ca25 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220324.cpp) +ADD_EXECUTABLE(2203 220324-CN.cpp)