主要考察对矩阵的枚举编程功力。进行遍历,然后对每个位置进行计算结果。
每个位置的计算考虑行、列的下标及步长
如果下标大于0,起始下标步长会是3,否则起始位置为下标-=1,步长为2, 代码:
public static int[][] imageSmoother(int[][] img) {
int[][] res = new int[img.length][img[0].length];
for (int row = 0; row < img.length; row++) {
for (int column = 0; column < img[0].length; column++) {
res[row][column] = count(img, row, column);
}
}
return res;
}
private static int count(int[][] img, int row, int column) {
int rowLen, columnLen;
//对行和列确定起始遍历下标和步长:
// 如果大于0,起始下标步长会是3,
// 如果当前下标不大于0,起始位置为下标-=1,步长为2,
if (row > 0) {
row -= 1;
rowLen = 3;
} else {
rowLen = 2;
}
if (column > 0) {
column -= 1;
columnLen = 3;
} else {
columnLen = 2;
}
int count = 0;
int times = 0;
//计算结果
for (int i = row; i - row < rowLen && i < img.length; i++) {
for (int j = column; j - column < columnLen && j < img[0].length; j++) {
count += img[i][j];
times++;
}
}
return count / times;
}



