# My AC java O(1) solution (easy to read)

• ``````public class Solution {
public void setZeroes(int[][] matrix) {
boolean fr = false,fc = false;
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++) {
if(matrix[i][j] == 0) {
if(i == 0) fr = true;
if(j == 0) fc = true;
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
for(int i = 1; i < matrix.length; i++) {
for(int j = 1; j < matrix[0].length; j++) {
if(matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
if(fr) {
for(int j = 0; j < matrix[0].length; j++) {
matrix[0][j] = 0;
}
}
if(fc) {
for(int i = 0; i < matrix.length; i++) {
matrix[i][0] = 0;
}
}

}
``````

}

• Better more explanation, like fc means first column, fr means first row.

• Explanation

``````    // fr = first row
// fc = first col

// Use first row and first column as markers.
// if matrix[i][j] = 0, mark respected row and col marker = 0; indicating
that later this respective row and col must be marked 0;
// And because you are altering first row and collumn,
you need to  have two variables to track their own status.
// So, for ex, if any one of the first row is 0, fr = 0,
and at the end set all first row to 0;
``````

• Best explanation for this problem.

• Clear and concise, thumbs up!

• just the same as mine.

• what is the time complexity of this problem?

• @dreamzyx It's apparently O(m*n)

• @jaqenhgar Thanks for the explanation

• Why does the subject say "O(1)" ? Can someone please explain ?

• @lz2343 clean code. nice!

• @nxg It's O(1) space.

• // fr = first row
// fc = first col

``````// Use first row and first column as markers.
// if matrix[i][j] = 0, mark respected row and col marker = 0; indicating
that later this respective row and col must be marked 0;
// And because you are altering first row and collumn,
you need to  have two variables to track their own status.
// So, for ex, if any one of the first row is 0, fr = 0,
``````

Will be better if you can attach a picture of it

• This post is deleted!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.