Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/442702007275913216
added 5 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Set Matrix Zerosmatrix zeros

I solved this problem in about 11 minutes  (with all 50+ test cases passed).

I think most of you already know me by now, and I truly appreciate your advice, but please be brutal and judge me as if I was being interviewed by you.

Question:

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Code:

public void setZeroes(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; boolean[][] flagArr = new boolean[rows][cols]; for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ if(matrix[i][j]==0){ flagArr[i][j]=true; } } } for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ if(flagArr[i][j]==true){ /*for rows*/ for(int k=0; k<rows; k++){ matrix[k][j]=0; } /*for cols*/ for(int z=0; z<cols; z++){ matrix[i][z]=0; } } } } } 

Set Matrix Zeros

I solved this problem in about 11 minutes(with all 50+ test cases passed).

I think most of you already know me by now, and I truly appreciate your advice, but please be brutal and judge me as if I was being interviewed by you.

Question:

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

public void setZeroes(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; boolean[][] flagArr = new boolean[rows][cols]; for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ if(matrix[i][j]==0){ flagArr[i][j]=true; } } } for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ if(flagArr[i][j]==true){ /*for rows*/ for(int k=0; k<rows; k++){ matrix[k][j]=0; } /*for cols*/ for(int z=0; z<cols; z++){ matrix[i][z]=0; } } } } } 

Set matrix zeros

I solved this problem in about 11 minutes  (with all 50+ test cases passed).

I think most of you already know me by now, and I truly appreciate your advice, but please be brutal and judge me as if I was being interviewed by you.

Question:

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Code:

public void setZeroes(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; boolean[][] flagArr = new boolean[rows][cols]; for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ if(matrix[i][j]==0){ flagArr[i][j]=true; } } } for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ if(flagArr[i][j]==true){ /*for rows*/ for(int k=0; k<rows; k++){ matrix[k][j]=0; } /*for cols*/ for(int z=0; z<cols; z++){ matrix[i][z]=0; } } } } } 
Source Link
bazang
  • 2.2k
  • 5
  • 23
  • 32

Set Matrix Zeros

I solved this problem in about 11 minutes(with all 50+ test cases passed).

I think most of you already know me by now, and I truly appreciate your advice, but please be brutal and judge me as if I was being interviewed by you.

Question:

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

public void setZeroes(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; boolean[][] flagArr = new boolean[rows][cols]; for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ if(matrix[i][j]==0){ flagArr[i][j]=true; } } } for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++){ if(flagArr[i][j]==true){ /*for rows*/ for(int k=0; k<rows; k++){ matrix[k][j]=0; } /*for cols*/ for(int z=0; z<cols; z++){ matrix[i][z]=0; } } } } }