设 grid 的大小为 n \times n,那么我们申请一个大小为 (n - 2) \times (n - 2) 的矩阵 res 用来存放答案。我们遍历 grid 中每个 3 \times 3 子矩阵的左上角,然后统计当前子矩阵的最大值放入 res 中。
具体做法是,我们顺序遍历 i(0 \le i \lt n - 2),再顺序遍历 j(0 \le j \lt n - 2),接着遍历求解 {\textit{grid}(x, y)|i \le x \lt i + 3, j \le y \lt j + 3\ 的最大值放入 res}[i][j] 中。
代码
[sol1-Python3]
1 2 3 4 5 6 7 8
classSolution: deflargestLocal(self, grid: List[List[int]]) -> List[List[int]]: n = len(grid) ans = [[0] * (n - 2) for _ inrange(n - 2)] for i inrange(n - 2): for j inrange(n - 2): ans[i][j] = max(grid[x][y] for x inrange(i, i + 3) for y inrange(j, j + 3)) return ans
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: vector<vector<int>> largestLocal(vector<vector<int>>& grid) { int n = grid.size(); vector<vector<int>> res(n - 2, vector<int>(n - 2, 0)); for (int i = 0; i < n - 2; i++) { for (int j = 0; j < n - 2; j++) { for (int x = i; x < i + 3; x++) { for (int y = j; y < j + 3; y++) { res[i][j] = max(res[i][j], grid[x][y]); } } } } return res; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicint[][] largestLocal(int[][] grid) { intn= grid.length; int[][] res = newint[n - 2][n - 2]; for (inti=0; i < n - 2; i++) { for (intj=0; j < n - 2; j++) { for (intx= i; x < i + 3; x++) { for (inty= j; y < j + 3; y++) { res[i][j] = Math.max(res[i][j], grid[x][y]); } } } } return res; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassSolution { publicint[][] LargestLocal(int[][] grid) { int n = grid.Length; int[][] res = newint[n - 2][]; for (int i = 0; i < n - 2; i++) { res[i] = newint[n - 2]; for (int j = 0; j < n - 2; j++) { for (int x = i; x < i + 3; x++) { for (int y = j; y < j + 3; y++) { res[i][j] = Math.Max(res[i][j], grid[x][y]); } } } } return res; } }
int** largestLocal(int** grid, int gridSize, int* gridColSize, int* returnSize, int** returnColumnSizes) { int **res = (int **)malloc(sizeof(int *) * (gridSize - 2)); for (int i = 0; i < gridSize - 2; i++) { res[i] = (int *)calloc(gridSize - 2, sizeof(int)); } for (int i = 0; i < gridSize - 2; i++) { for (int j = 0; j < gridSize - 2; j++) { for (int x = i; x < i + 3; x++) { for (int y = j; y < j + 3; y++) { res[i][j] = MAX(res[i][j], grid[x][y]); } } } } *returnSize = gridSize - 2; *returnColumnSizes = (int *)calloc(gridSize - 2, sizeof(int)); for (int i = 0; i < gridSize - 2; i++) { (*returnColumnSizes)[i] = gridSize - 2; } return res; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var largestLocal = function(grid) { const n = grid.length; const res = newArray(n - 2).fill(0).map(() =>newArray(n - 2).fill(0)); for (let i = 0; i < n - 2; i++) { for (let j = 0; j < n - 2; j++) { for (let x = i; x < i + 3; x++) { for (let y = j; y < j + 3; y++) { res[i][j] = Math.max(res[i][j], grid[x][y]); } } } } return res; };
funclargestLocal(grid [][]int) [][]int { n := len(grid) ans := make([][]int, n-2) for i := 1; i < n-1; i++ { row := make([]int, n-2) for j := 1; j < n-1; j++ { mx := grid[i][j] for r := i - 1; r <= i+1; r++ { for c := j - 1; c <= j+1; c++ { if grid[r][c] > mx { mx = grid[r][c] } } } row[j-1] = mx } ans[i-1] = row } return ans }