661. Image Smoother

原题链接: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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值