leetcode刷題記錄 661~

maoguai發表於2024-10-14

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

相關文章