当前位置:首页 > 编程笔记 > 正文
已解决

【LeetCode热题100】--73.矩阵置零

来自网友在路上 157857提问 提问时间:2023-09-26 06:27:15阅读次数: 57

最佳答案 问答题库578位专家为你答疑解惑

73.矩阵置零

image-20230925104336359

使用标记数组:

使用两个标记数组分别记录每一行和每一列是否有零出现

先遍历一次数组,如果某个元素为0,则将该元素所在的行和列所对应的标记数组的位置为true,最后再遍历该数组,用标记数组更新原数即可

class Solution {public void setZeroes(int[][] matrix) {int m = matrix.length;  //矩阵的行数int n = matrix[0].length;  //矩阵的列数boolean[] row = new boolean[m];boolean[] col = new boolean[n];for(int i = 0;i<m;i++){for(int j = 0; j<n ;j++){if(matrix[i][j] == 0){row[i] = col[j] =true;}}}for(int i = 0;i<m;i++){for(int j = 0;j<n;j++){if(row[i] || col[j]){matrix[i][j] = 0;}}}}
}
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"【LeetCode热题100】--73.矩阵置零":http://eshow365.cn/6-13738-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!