LeetCode 661. 图片平滑器(C、C++、python)


包含整数的二维矩阵 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]。

C

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *columnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** imageSmoother(int** M, int MRowSize, int MColSize, int** columnSizes, int* returnSize) 
{
    int m=MRowSize;
    int n=MColSize;
    *returnSize=m;
    int** res=(int**)malloc(sizeof(int*)*m);
    *columnSizes=(int*)malloc(sizeof(int)*m);
    int Row,Col;
    int count,temp;
    for(int i=0;i<m;i++)
    {
        res[i]=(int*)malloc(sizeof(int)*n);
        for(int j=0;j<n;j++)
        {
            count=0;
            temp=0;
            for(int r=-1;r<=1;r++)
            {
                for(int c=-1;c<=1;c++)
                {
                    Row=i+r;
                    Col=j+c;
                    if(Row>=0 && Row<m && Col>=0 && Col<n)
                    {
                        count++;
                        temp+=M[Row][Col];
                    }
                }
            }
            res[i][j]=temp/count;
        }
        (*columnSizes)[i]=n;
    }
    return res;
}

C++

class Solution {
public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& M) 
    {
        int m=M.size();      //行数
        int n=M[0].size();   //列数
        vector<vector<int>> res(m,vector<int>(n));
        int temp;
        int count;
        int row,col;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                count=0;
                temp=0;
                for(int r=-1;r<=1;r++)
                {
                    for(int c=-1;c<=1;c++)
                    {
                        row=i+r;
                        col=j+c;
                        if(row>=0 && row<m && col>=0 && col<n)
                        {
                            count++;
                            temp+=M[row][col];
                        }
                    }
                }
                res[i][j]=temp/count;
            }
        }
        return res;
        
        
    }
};

python

class Solution:
    def imageSmoother(self, M):
        """
        :type M: List[List[int]]
        :rtype: List[List[int]]
        """
        m=len(M)
        n=len(M[0])
        res=[[0 for i in range(0,n)] for j in range(0,m)]
        for i in range(0,m):
            for j in range(0,n):
                count=0
                temp=0
                for r in range(-1,2):
                    for c in range(-1,2):
                        row=i+r
                        col=j+c
                        if row>=0 and row<m and col>=0 and col<n:
                            count+=1
                            temp+=M[row][col]
                res[i][j]=temp//count
        return res;
        

猜你喜欢

转载自blog.csdn.net/qq_27060423/article/details/82872389
今日推荐