classSolution: defmodifiedMatrix(self, matrix: List[List[int]]) -> List[List[int]]: m, n = len(matrix), len(matrix[0]) mx = [max([matrix[i][j] for i inrange(m)]) for j inrange(n)] return [[matrix[i][j] if matrix[i][j] != -1else mx[j] for j inrange(n)] for i inrange(m)]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) { int m = matrix.size(), n = matrix[0].size(); vector<int> mx(n, INT_MIN); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) mx[j] = max(mx[j], matrix[i][j]); vector<vector<int>> ans(m, vector<int>(n)); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) ans[i][j] = (matrix[i][j] == -1) ? mx[j] : matrix[i][j]; return ans; } };
寫在最後
Cover photo is remixed from @吃肥皂泡泡, thanks for their work!