661. 图片平滑器

地址:

力扣icon-default.png?t=LBL2https://leetcode-cn.com/problems/image-smoother/

题目:

包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们。

示例 1:

输入:
[[1,1,1],
 [1,0,1],
 [1,1,1]]
输出:
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]
解释:
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0


注意:

给定矩阵中的整数范围为 [0, 255]。
矩阵的长和宽的范围均为 [1, 150]。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/image-smoother
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:

初看题目不知所云,在同学的解释下明白

就是求与当前元素 相邻 的元素值,算的平均值放入

方法一、遍历取值

int **myMalloc(int r, int c, int *return_r, int **return_c)
{
	int **ret = (int **)malloc(sizeof(int *) * r);
	*return_r = r;
	
	*return_c =(int *)malloc(sizeof(int) * r); 
	for(int i=0; i<r; i++)
	{
		ret[i] = (int *)malloc(sizeof(int) * c);
		(*return_c)[i] = c;
	}

    return ret;
}

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** imageSmoother(int** img, int imgSize, int* imgColSize, int* returnSize, int** returnColumnSizes){
    int r = imgSize;
    int c = imgColSize[0];
    int i,j;
    int nums = 0, total = 0, avg = 0;
    int **ret = myMalloc(r, c, returnSize, returnColumnSizes);

    /* quiet like the islandPerimeter question */
    for(i=0; i<r; i++)
    {
        /*
        i-1 >=0
        j-1 >= 0
        i+1 < r
        j+1 < c

        [i-1][j-1]    [i-1][j]    [i-1][j+1]
        [i][j-1]      [i][j]      [i][j+1]
        [i+1][j-1]    [i+1][j]    [i+1][j+1]
        */
        for(j=0; j<c; j++)
        {
            nums = 0;
            total = 0;

            //printf("checking-------[%d[%d]----\n", i, j);
            // 1. check right side
            if( (j+1) < c)
            {
                nums++;
                total += img[i][j+1];
                //printf("1.right [%d[%d]=[%d]...num=[%d], total=[%d]", i, j+1, img[i][j+1], nums, total);
            }

            // 2. check right-down side
            if((i+1) < r && (j+1) <c)
            {
                nums++;
                total += img[i+1][j+1];
                //printf("2.right-down [%d[%d]=[%d]...num=[%d], total=[%d]", i+1, j+1, img[i+1][j+1], nums, total);
            }

            // 3. check down side
            if((i+1) < r)
            {
                nums++;
                total += img[i+1][j];
                //printf("3.down [%d[%d]=[%d]...num=[%d], total=[%d]", i+1, j, img[i+1][j], nums, total);
            }

            // 4. check left-down side
            if((i+1) < r && (j-1) >= 0)
            {
                nums++;
                total += img[i+1][j-1];
                //printf("4.left-down [%d[%d]=[%d]...num=[%d], total=[%d]", i+1, j-1, img[i+1][j-1], nums, total);
            }

            // 5. check left side
            if((j-1) >= 0)
            {
                nums++;
                total += img[i][j-1];
                //printf("5.left [%d[%d]=[%d]...num=[%d], total=[%d]", i, j-1, img[i][j-1], nums, total);
            }

            // 6. check left-up side
            if((i-1) >= 0 && (j-1) >= 0)
            {
                nums++;
                total += img[i-1][j-1];
                //printf("6.left-up [%d[%d]=[%d]...num=[%d], total=[%d]", i-1, j-1, img[i-1][j-1], nums, total);
            }

            // 7. check up side
            if((i-1) >= 0)
            {
                nums++;
                total += img[i-1][j];
                //printf("7.up [%d[%d]=[%d]...num=[%d], total=[%d]", i-1, j, img[i-1][j], nums, total);
            }

            // 8. check up-right side
            if((i-1) >= 0 && (j+1) < c)
            {
                nums++;
                total += img[i-1][j+1];
                //printf("8.up-right [%d[%d]=[%d]...num=[%d], total=[%d]", i-1, j+1, img[i-1][j+1], nums, total);
            }

            // 9. itself
            nums++;
            total += img[i][j];
            //printf("9.itselt [%d[%d]=[%d]...num=[%d], total=[%d]", i, j, img[i][j], nums, total);

            avg = total / nums;
            //printf("avg=[%d]\n", avg);

            ret[i][j] = avg;
        }
    }

    return ret;
}

查看更多刷题笔记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值