资讯专栏INFORMATION COLUMN

leetcode 661 Image Smoother

Blackjun / 3053人阅读

摘要:题目详情输入一个整数二维数组,输出一个大小相同的二维数组,其中每个元素的值,代表着输入数组同样位置的元素的周围元素的平均值包括自己。思路特殊情况就是矩阵的四角的相邻元素只有个。其余的处于最外围一圈的元素的相邻元素只有个。

题目详情
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.

输入一个整数二维数组,输出一个大小相同的二维数组,其中每个元素的值,代表着输入数组M同样位置的元素的周围元素的平均值(包括自己)。

Example 1:
Input:
[[1,1,1],
[1,0,1],
[1,1,1]]
Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0

思路

特殊情况就是矩阵的四角的相邻元素只有3个。

其余的处于最外围一圈的元素的相邻元素只有5个。

解法
    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[][] res = new int[rows][cols];
        
        for(int row=0;row= 0 && x < row && y >= 0 && y < col;
        
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68430.html

相关文章

  • leetcode 部分解答索引(持续更新~)

    摘要:前言从开始写相关的博客到现在也蛮多篇了。而且当时也没有按顺序写现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。顺序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 从开始写leetcode相关的博客到现在也蛮多篇了。而且当时也没有按顺序写~现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。 顺序整理 1~50 1...

    leo108 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • 70道前端LeetCode题目集合及视频讲解(持续更新中...)

    前端LeetCode刷题 下面是已刷的题目的目录。GitHub:https://github.com/cunzaizhuy...每日打卡更新中,欢迎关注。 数组类 26 删除排序数组中的重复项 27 移除元素 35 搜索插入位置 66 加1 80 medium 删除排序数组中的重复项2 88 合并两个有序数组 167 两数之和II - 输入有序数组 118 杨辉三角 169 easy 求众数 1...

    mayaohua 评论0 收藏0
  • [LeetCode] 733. Flood Fill

    Problem An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row a...

    church 评论0 收藏0
  • LeetCode[48] Rotate Image

    LeetCode[48] Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 复杂度O(N^2),O(1) 代码 public void ro...

    sanyang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<