Set Matrix Zeroes
Given an r x c matrix. If an element is 0, set its entire row and column to 0. Do it in-place.
Example 1:
Input: matrix = [[1,1,1]
,[1,0,1],
[1,1,1]]
Output: [[1,0,1]
,[0,0,0]
,[1,0,1]]
Example 2:
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
c++ implementation:
void setZeroes(vector<vector<int>>& m) {
int r=m.size(); int c=m[0].size();
int x[r];int y[c];
for(int i=0;i<r;i++)
x[i]=1;
for(int j=0;j<c;j++)
y[j]=1;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(m[i][j]==0)
{
x[i]=y[j]=0;
}
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(x[i] ==0 || y[j]==0)
{
m[i][j]=0;
}
}
}
}
No comments