classSolution { public: vector<vector<int>> imageSmoother(vector<vector<int>>& img) { int m = img.size(), n = img[0].size(); vector<vector<int>> ret(m, vector<int>(n)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int num = 0, sum = 0; for (int x = i - 1; x <= i + 1; x++) { for (int y = j - 1; y <= j + 1; y++) { if (x >= 0 && x < m && y >= 0 && y < n) { num++; sum += img[x][y]; } } } ret[i][j] = sum / num; } } return ret; } };
classSolution { publicint[][] imageSmoother(int[][] img) { intm= img.length, n = img[0].length; int[][] ret = newint[m][n]; for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { intnum=0, sum = 0; for (intx= i - 1; x <= i + 1; x++) { for (inty= j - 1; y <= j + 1; y++) { if (x >= 0 && x < m && y >= 0 && y < n) { num++; sum += img[x][y]; } } } ret[i][j] = sum / num; } } return ret; } }
publicclassSolution { publicint[][] ImageSmoother(int[][] img) { int m = img.Length, n = img[0].Length; int[][] ret = newint[m][]; for (int i = 0; i < m; i++) { ret[i] = newint[n]; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int num = 0, sum = 0; for (int x = i - 1; x <= i + 1; x++) { for (int y = j - 1; y <= j + 1; y++) { if (x >= 0 && x < m && y >= 0 && y < n) { num++; sum += img[x][y]; } } } ret[i][j] = sum / num; } } return ret; } }
int** imageSmoother(int** img, int imgSize, int* imgColSize, int* returnSize, int** returnColumnSizes){ int m = imgSize, n = imgColSize[0]; int ** ret = (int **)malloc(sizeof(int *) * m); *returnColumnSizes = (int *)malloc(sizeof(int) * m); for (int i = 0; i < m; i ++) { ret[i] = (int *)malloc(sizeof(int) * n); memset(ret[i], 0, sizeof(int) * n); (*returnColumnSizes)[i] = n; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int num = 0, sum = 0; for (int x = i - 1; x <= i + 1; x++) { for (int y = j - 1; y <= j + 1; y++) { if (x >= 0 && x < m && y >= 0 && y < n) { num++; sum += img[x][y]; } } } ret[i][j] = sum / num; } } *returnSize = m; return ret; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var imageSmoother = function(img) { const m = img.length, n = img[0].length; const ret = newArray(m).fill(0).map(() =>newArray(n).fill(0)); for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { let num = 0, sum = 0; for (let x = i - 1; x <= i + 1; x++) { for (let y = j - 1; y <= j + 1; y++) { if (x >= 0 && x < m && y >= 0 && y < n) { num++; sum += img[x][y]; } } } ret[i][j] = Math.floor(sum / num); } } return ret; };
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defimageSmoother(self, img: List[List[int]]) -> List[List[int]]: m, n = len(img), len(img[0]) ans = [[0] * n for _ inrange(m)] for i inrange(m): for j inrange(n): tot, num = 0, 0 for x inrange(max(i - 1, 0), min(i + 2, m)): for y inrange(max(j - 1, 0), min(j + 2, n)): tot += img[x][y] num += 1 ans[i][j] = tot // num return ans
funcimageSmoother(img [][]int) [][]int { m, n := len(img), len(img[0]) ans := make([][]int, m) for i := range ans { ans[i] = make([]int, n) for j := range ans[i] { sum, num := 0, 0 for _, row := range img[max(i-1, 0):min(i+2, m)] { for _, v := range row[max(j-1, 0):min(j+2, n)] { sum += v num++ } } ans[i][j] = sum / num } } return ans }
funcmax(a, b int)int { if b > a { return b } return a }
funcmin(a, b int)int { if a > b { return b } return a }
复杂度分析
时间复杂度:O(mnC^2),其中 m 为给定矩阵的行数,n 为给定矩阵的列数,C=3 为过滤器的宽高。我们需要遍历整个矩阵以计算每个位置的值,计算单个位置的值的时间复杂度为 O(C^2)。