leetcode刷题记录 661~

maoguai / 2024-10-15 / 原文

661 easy

题目

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

思路

遍历移动
时间复杂度O(mn) 空间复杂度O(mn)

代码

点击查看代码
class Solution:
    def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
        col = len(img)
        row = len(img[0])
        ava = [[0 for i in range(row)] for j in range(col)]
        index = [-1,0,1]
        for i in range(col):
            for j in range(row):
                count = 0
                tmp   = 0
                for x in index:
                    if i + x >= 0 and i+x < col:
                        for y in index :
                            if j + y >=0 and j+y < row:
                                count += 1
                                tmp += img[i + x][j + y]
                ava[i][j] = int(tmp/count)
        return ava