Given a matrix, if an element is 0 , set all elements in its row and column to 0 . Please use the in-place algorithm .m x n
Idea 1: Record row and column values
void setZeroes(int** matrix, int matrixSize, int* matrixColSize){
int row[matrixSize],col[*matrixColSize];
for(int i = 0;i<matrixSize;i++)row[i]=0;
for(int i = 0;i<*matrixColSize;i++)col[i] = 0;
for(int i = 0;i<matrixSize;i++)
{
for(int j = 0;j<*matrixColSize;j++)
{
if(matrix[i][j]==0)
{
row[i] = 1;
col[j] = 1;
}
}
}
for(int i = 0;i<matrixSize;i++)
{
if(row[i]==1)
{
for(int j = 0;j<*matrixColSize;j++)
{
matrix[i][j] = 0;
}
}
}
for(int i = 0;i<*matrixColSize;i++)
{
if(col[i]==1)
{
for(int j = 0;j<matrixSize;j++)
{
matrix[j][i] = 0;
}
}
}
return matrix;
}
analyze:
In this question, you can directly record the row and column values, and then change all the numbers in the row or column to 0 according to whether the numbers in the row and column values are 1
Summarize:
This question examines the application of the matrix, which can be solved directly by violence.