leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0661.cpp (809B)


      1 class Solution {
      2   public:
      3     vector<vector<int>> imageSmoother(vector<vector<int>> &img) const {
      4         const int n = img.size() - 1;
      5         const int m = img[0].size() - 1;
      6         for (int i = 0; i <= n; i++) {
      7             for (int j = 0; j <= m; j++) {
      8                 int sum = 0, count = 0;
      9                 for (int k = max(i - 1, 0); k <= min(i + 1, n); k++) {
     10                     for (int l = max(j - 1, 0); l <= min(j + 1, m); l++) {
     11                         sum += img[k][l] & 0xFF;
     12                         count++;
     13                     }
     14                 }
     15                 img[i][j] |= (sum / count) << 8;
     16             }
     17         }
     18         for (int i = 0; i <= n; i++) {
     19             for (int j = 0; j <= m; j++) {
     20                 img[i][j] >>= 8;
     21             }
     22         }
     23         return img;
     24     }
     25 };