原题链接:https://leetcode.com/problems/image-smoother/description/
import java.util.Arrays;
/**
* Created by Joe on 2018/3/15.
* 661. Image Smoother
* https://leetcode.com/problems/image-smoother/description/
*/
public class P661 {
private int[][] directions = new int[][] {
{-1, 1}, //左上
{0, 1}, //正上
{1, 1}, //右上
{-1, 0}, //左
{1, 0}, //右
{-1, -1}, //左下
{0, -1}, //正下
{1, -1}, //右下
};
public int[][] imageSmoother(int[][] M) {
int m = M.length;
int n = M[0].length;
int[][] smoother = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
smoother[i][j] = getValue(M, i, j, m, n);
}
}
return smoother;
}
private int getValue(int[][] M, int x, int y, int m, int n) {
int count = 1;
int value = M[x][y];
for (int[] direction : directions) {
int newX = x + direction[0];
int newY = y + direction[1];
if (check(newX, newY, m, n)) {
value += M[newX][newY];
count++;
}
}
return value / count;
}
private boolean check(int x, int y, int rows, int cols) {
return x >= 0 && x < rows && y >= 0 && y < cols;
}
public static void main(String[] args) {
int[][] m = new int[][]{
{1,1,1}, {1,0,1}, {1,1,1}
};
int[][] smoother = new P661().imageSmoother(m);
for (int[] s : smoother) {
System.out.println(Arrays.toString(s));
}
}
}
我的代码还是有些臃肿,比如我是穷举的方向,还有功能责任划分太细,实际上并没有让我的代码变的更加简洁。
在评论区看到一个不错的代码,贴上来用于对比学习:
public class ImageSmoother {
public int[][] imageSmoother(int[][] M) {
if (M == null) return null;
int rows = M.length;
if (rows == 0) return new int[0][];
int cols = M[0].length;
int result[][] = new int[rows][cols];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int count = 0;
int sum = 0;
for (int incR : new int[]{-1, 0, 1}) {
for (int incC : new int[]{-1, 0, 1}) {
if (isValid(row + incR, col + incC, rows, cols)) {
count++;
sum += M[row + incR][col + incC];
}
}
}
result[row][col] = sum / count;
}
}
return result;
}
private boolean isValid(int x, int y, int rows, int cols) {
return x >= 0 && x < rows && y >= 0 && y < cols;
}
}