0
点赞
收藏
分享

微信扫一扫

661. 图片平滑器

>661. 图片平滑器<

>imageSmoother<

题目描述


一、解题思路

1、解法一( Java )

解法思路:方向数组 + 模拟遍历
定义方向数组 DIRint x = i + dir[k][0]int nI = i + dir[0] 为原始坐标加上横坐标偏移量,int y = j + dir[k][1]int nJ = j + dir[1] 为原始坐标加上纵坐标偏移量,对于位置 (i,j) ,我们枚举其周围的九个单元是否存在,对于存在的单元格,我们统计其数量 cnt 与总和 sum,那么该位置平滑处理后的结果即为sum / cnt,即直接依次计算每一个位置平滑处理后的结果即可。

伪代码如下:

class Solution{
	//定义方向数组
    private static int[][] DIR = new int[][]{
            {-1, -1}, {-1, 0}, {-1, 1},
            {0, -1}, {0, 0}, {0, 1},
            {1, -1}, {1, 0}, {1, 1}
    };

    public int[][] imageSmoother(int[][] img) {
        int m = img.length, n = img[0].length;
        int[][] ans = new int[m][n];
        for (int i = 0; i < img.length; i++) {
            for (int j = 0; j < img[0].length; j++) {
                int sum = 0, cnt = 0;
                for (int k = 0; k < 9; k++) {
                    int x = i + DIR[k][0];//原始坐标加上横坐标偏移量
                    int y = j + DIR[k][1];//原始坐标加上纵坐标偏移量
                    if (x < 0 || y < 0 || x == m || y == n) {
                        continue;
                    }
                    sum += img[x][y];
                    cnt++;
                }
                ans[i][j] = sum / cnt;
            }

        }
        return ans;
    }
}
class Solution{
	//定义方向数组
    private static int[][] DIRS = new int[][]{
            {-1, -1}, {-1, 0}, {-1, 1},
            {0, -1}, {0, 0}, {0, 1},
            {1, -1}, {1, 0}, {1, 1}
    };

    public int[][] imageSmoother(int[][] img) {
        int m = img.length, n = img[0].length;
        int[][] ans = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                ans[i][j] = manager(img, i, j);
            }
        }
        return ans;
    }

    private int manager(int[][] oldImg, int i, int j) {
        int sum = 0, cnt = 0;
        for (int[] dir : DIRS) {
            int nI = i + dir[0];//原始坐标加上横坐标偏移量
            int nJ = j + dir[1];//原始坐标加上纵坐标偏移量
            if (nI >= 0 && nI < oldImg.length && nJ >= 0 && nJ < oldImg[0].length) {
                cnt += 1;
                sum += oldImg[nI][nJ];
            }
        }

        return sum / cnt;
    }
}

完整代码如下:

/**
 * @author Listen 1024
 * @description
 * @date 2022-03-25 9:13
 */
public class 图片平滑器_661 {
    public static void main(String[] args) {
        int[][] old = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
        Solution6 solution6 = new Solution6();
        int[][] ans = solution6.imageSmoother(old);
        for (int i = 0; i < ans.length; i++) {
            for (int j = 0; j < ans[0].length; j++) {
                System.out.print(ans[i][j] + " ");

            }
            System.out.println();
        }
    }
}


class Solution6 {
    private static int[][] DIR = new int[][]{
            {-1, -1}, {-1, 0}, {-1, 1},
            {0, -1}, {0, 0}, {0, 1},
            {1, -1}, {1, 0}, {1, 1}
    };

    public int[][] imageSmoother(int[][] img) {
        int m = img.length, n = img[0].length;
        int[][] ans = new int[m][n];
        for (int i = 0; i < img.length; i++) {
            for (int j = 0; j < img[0].length; j++) {
                int sum = 0, cnt = 0;
                for (int k = 0; k < 9; k++) {
                    int x = i + DIR[k][0];
                    int y = j + DIR[k][1];
                    if (x < 0 || y < 0 || x == m || y == n) {
                        continue;
                    }
                    sum += img[x][y];
                    cnt++;
                }
                ans[i][j] = sum / cnt;
            }

        }
        return ans;
    }
}

运行结果截图如下:
代码运行结果截图

举报

相关推荐

0 条评论