首页 > 代码库 > LeetCode-Set Matrix Zeroes

LeetCode-Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
public class Solution {    public void setZeroes(int[][] matrix) {        if(matrix==null){            return;        }        int m=matrix.length;        int n=matrix[0].length;                List<Integer> r=new ArrayList<Integer>();        List<Integer> c=new ArrayList<Integer>();        for(int i=0; i<m; i++){            for(int j=0; j<n; j++){                if(matrix[i][j]==0){                    r.add(i);                    c.add(j);                }            }        }        for(int i=0; i<r.size(); i++){            int row=r.get(i);            int column=c.get(i);            for(int k=0; k<n; k++){                matrix[row][k]=0;            }            for(int l=0; l<m; l++){                matrix[l][column]=0;            }        }    }}

 

LeetCode-Set Matrix Zeroes