集思吧网站怎么做问卷,集团公司网站模板,厦门微信网站,公司商标标志设计Leetcode 3033. Modify the Matrix 1. 解题思路2. 代码实现 题目链接#xff1a;3033. Modify the Matrix
1. 解题思路
这一题是一道easy的题目#xff0c;整体思路上没啥难度#xff0c;就是按照题目翻译一下即可#xff0c;先遍历一下找到每一列的最大元素#xff0c…Leetcode 3033. Modify the Matrix 1. 解题思路2. 代码实现 题目链接3033. Modify the Matrix
1. 解题思路
这一题是一道easy的题目整体思路上没啥难度就是按照题目翻译一下即可先遍历一下找到每一列的最大元素然后把所有值为-1的元素替换为该列的最大元素即可。
2. 代码实现
给出python代码实现如下
class Solution:def modifiedMatrix(self, matrix: List[List[int]]) - List[List[int]]:n, m len(matrix), len(matrix[0])_max [-1 for _ in range(m)]to_change []for i in range(n):for j in range(m):if matrix[i][j] -1:to_change.append((i, j))else:_max[j] max(_max[j], matrix[i][j])for i, j in to_change:matrix[i][j] _max[j]return matrix提交代码评测得到耗时87ms占用内存16.6MB。