Java solution to with O(1)space

• just use the edge thingking to think about this question, while the num in the array equals 0, make the first num of its row and column to 0,so that can not use the extra space

``````public void setZeroes(int[][] matrix) {
boolean isFirstRowZero = false;
boolean isFirstColumnZero  = false;
for(int i = 0; i < matrix.length ; i++)
{
if(matrix[i][0] == 0)
{
isFirstColumnZero  = true;
}
}
for(int i = 0; i < matrix[0].length; i++)
{
if(matrix[0][i] == 0)
{
isFirstRowZero = true;
}
}
for(int i = 1; i< matrix.length;i++)
{
for(int j = 1;  j < matrix[i].length; j++)
{
if(matrix[i][j] == 0)
{
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for(int i = 1; i< matrix.length;i++)
{
if(matrix[i][0] == 0)
{
for(int j = 0;  j < matrix[i].length; j++)
{
matrix[i][j] = 0;
}
}
}
for(int i = 1; i< matrix[0].length;i++)
{
if(matrix[0][i] == 0)
{
for(int j = 0;  j < matrix.length; j++)
{
matrix[j][i] = 0;
}
}
}
if(isFirstRowZero)
{
for(int i = 0; i< matrix[0].length;i++)
{
matrix[0][i] = 0;
}
}
if(isFirstColumnZero )
{
for(int i = 0; i< matrix.length;i++)
{
matrix[i][0] = 0;
}
}
}``````

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