首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode题解——661/997

Leetcode题解——661/997

作者头像
出其东门
发布2019-07-19 11:21:25
3340
发布2019-07-19 11:21:25
举报
文章被收录于专栏:01二进制01二进制

661 图片平滑器

题目

https://leetcode-cn.com/problems/image-smoother/

题解

本题类似于深度学习中卷积神经网络的平均池化操作,本人采用的是暴力遍历的方法,先用8个变量表示其周围8个角,然后依次判断i,j和row和column之间的关系,如果合理,则参与计算并放到一个新的二维数组中,如此反复,最后返回该二维数组,代码如下:

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        int[][] M = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
        int[][] res = main.imageSmoother(M);
        for (int i = 0; i < res.length; i++) {
            for (int j = 0; j < res[i].length; j++) {
                System.out.print(res[i][j] + " ");
            }
            System.out.println();
        }
    }

    public int[][] imageSmoother(int[][] M) {
        int[][] res = new int[M.length][M[0].length];
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[i].length; j++) {
                res[i][j] = sum(M, i, j);
            }
        }
        return res;
    }

    public int sum(int[][] M, int i, int j) {
        int row = M.length;
        int column = M[0].length;
        int count = 1;
        int left_up = 0;
        int left = 0;
        int left_down = 0;
        int up = 0;
        int down = 0;
        int right_up = 0;
        int right = 0;
        int right_down = 0;
        if (i - 1 >= 0) {
            up = M[i - 1][j];
            count++;
            if (j - 1 >= 0) {
                left_up = M[i - 1][j - 1];
                count++;
            }
            if (j + 1 < column) {
                right_up = M[i - 1][j + 1];
                count++;
            }
        }
        if (i + 1 < row) {
            down = M[i + 1][j];
            count++;
            if (j - 1 >= 0) {
                left_down = M[i + 1][j - 1];
                count++;
            }
            if (j + 1 < column) {
                right_down = M[i + 1][j + 1];
                count++;
            }
        }
        if (j - 1 >= 0) {
            left = M[i][j - 1];
            count++;
        }
        if (j + 1 < column) {
            right = M[i][j + 1];
            count++;
        }
        int sum = left + left_down + left_up + right + right_down + right_up + up + down + M[i][j];
        return sum / count;
    }
}

997 找到小镇的法官

题目

https://leetcode-cn.com/problems/find-the-town-judge/

题解

用一个二维数组表示小镇中的每个人,二维数组的每一项都是一个长度为2的一维数组,分别表示相信他的人和他相信的人(即出入度),根据题目条件,只要相信他的人的个数为N-1以及他相信的人的个数为0即为法官,如果没有,返回-1

public int findJudge(int N, int[][] trust) {
    int[][] people = new int[N][2];
    for (int[] person : trust) {
        int out = person[0];
        int in = person[1];
        people[out - 1][0]++;
        people[in - 1][1]++;
    }
    for (int i = 0; i < N; i++) {
        if (people[i][0] == 0 && people[i][1] == N - 1)
            return i + 1;
    }
    return -1;
}


本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 01二进制 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 661 图片平滑器
    • 题目
      • 题解
      • 997 找到小镇的法官
        • 题目
          • 题解
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档