661. Image Smoother【easy】

661. Image Smoother【easy】

Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.

Example 1:

Input:
[[1,1,1],
 [1,0,1],
 [1,1,1]]
Output:
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0

Note:

  1. The value in the given matrix is in the range of [0, 255].
  2. The length and width of the given matrix are in the range of [1, 150].

错误解法:

 1 class Solution {
 2 public:
 3     vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
 4         int row = M.size();
 5         int col = M[0].size();
 6
 7         vector<vector<int>> temp(row + 1, vector<int>(col + 1));
 8         for (int j = 0; j < col + 1; ++j) {
 9             temp[0][j] = 0;
10         }
11         for (int i = 0; i < row + 1; ++i) {
12             temp[i][0] = 0;
13         }
14         for (int j = 0; j < col + 1; ++j) {
15             temp[row][j] = 0;
16         }
17         for (int i = 0; i < row + 1; ++i) {
18             temp[i][col] = 0;
19         }
20
21         for (int i = 1; i < row; ++i) {
22             for (int j = 1; j < col; ++j) {
23                 temp[i][j] = M[i - 1][j - 1];
24             }
25         }
26
27         for (int i = 1; i < row; ++i) {
28             for (int j = 1; j < col; ++j) {
29                 int sum = 0;
30                 for (int x = -1; x <= 1; ++x) {
31                     for (int y = -1; y <= 1; ++y) {
32                             sum += temp[i + x][j + y];
33                     }
34                 }
35
36                 temp[i][j] = floor(sum / 9);
37             }
38         }
39
40         vector<vector<int>> result(row, vector<int>(col));
41         for (int i = 0; i < row; ++i) {
42             for (int j = 0; j < col; ++j) {
43                 result[i][j] = temp[i + 1][j + 1];
44             }
45         }
46
47         return result;
48     }
49 };

一开始我还想取巧,把边界扩充,想着可以一致处理,但是发现没有审清题意,坑了啊!

解法一:

 1 class Solution {
 2 private:
 3     bool valid(int i,int j,vector<vector<int>>& M)
 4     {
 5         if (i >=0 && i<M.size() && j>=0 && j<M[0].size())
 6             return true;
 7         return false;
 8     }
 9
10 public:
11     vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
12         vector<vector<int>> res;
13         if (M.size()==0 || M[0].size()==0)
14             return res;
15
16         for (int i = 0; i< M.size(); i++)
17         {
18             vector<int> cur;
19             for(int j = 0; j< M[0].size(); j++)
20             {
21                 int total = 0;
22                 int count = 0;
23                 for (int x = -1; x<2;x++)
24                 {
25                     for (int y = -1; y<2; y++)
26                     {
27                         if(valid(i+x,j+y,M))
28                         {
29                             count++;
30                             total +=M[i+x][j+y];
31                         }
32                     }
33                 }
34                 cur.push_back(total/count);
35             }
36             res.push_back(cur);
37         }
38         return res;
39     }
40 };

中规中矩的解法,完全按照题目意思搞

解法三:

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

真正的大神解法!大神解释如下:Derived from StefanPochmann‘s idea in "game of life": the board has ints in [0, 255], hence only 8-bit is used, we can use the middle 8-bit to store the new state (average value), replace the old state with the new state by shifting all values 8 bits to the right.

时间: 2024-10-08 07:26:09

661. Image Smoother【easy】的相关文章

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

605. Can Place Flowers【easy】

605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represe

1. Two Sum【easy】

1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums

189. Rotate Array【easy】

189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 differen

459. Repeated Substring Pattern【easy】

459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters o

125. Valid Palindrome【easy】

125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. No

520. Detect Capital【easy】

520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds: All letters in this word are capitals, like "U

345. Reverse Vowels of a String【easy】

345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede&q