解题
class Solution:
def largestLocal(self, grid: list[list[int]]) -> list[list[int]]:
# li = []
# # 纵向移动
# for i in range(len(grid)):
# tem = []
# # 横向移动
# for j in range(len(grid)):
# # 是否可以构成 3 * 3 的矩阵
# if i + 2 < len(grid) and j + 2 < len(grid):
# # 3 * 3 矩阵中找出最大值
# tem.append(max(grid[i][j], grid[i][j + 1], grid[i][j + 2],
# grid[i + 1][j], grid[i + 1][j + 1], grid[i + 1][j + 2],
# grid[i + 2][j], grid[i + 2][j + 1], grid[i + 2][j + 2]))
# if len(tem) != 0:
# li.append(tem)
# return li
# 第二种
n = len(grid)
# 生成结果矩阵,用 0 填充, [0] * (n -2) 一行(n - 2) 个元素,共有 for _ in range(n-2) 行
ans = [[0] * (n - 2) for _ in range(n - 2)]
# 纵向移动
for i in range(n - 2):
# 横向移动
for j in range(n - 2):
# 找出 3 * 3 矩阵中的最大值
ans[i][j] = max(grid[x][y] for x in range(i, i + 3) for y in range(j, j + 3))
return ans
s = Solution()
l = [[9, 9, 8, 1], [5, 6, 2, 6], [8, 2, 6, 4], [6, 2, 2, 2]]
print(s.largestLocal(l))