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:
- The value in the given matrix is in the range of [0, 255].
- 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.