cracking the coding interview No1.7

来源:互联网 发布:淘宝卖家账号出售 编辑:程序博客网 时间:2024/05/16 02:03

1.7 Write an algorithm such that if an element in an M*N matrix is 0,its entire row and column are set to 0;

Answer:

void function(int matrix[][],int m,int n){if (matrix == NULL)return;int i,j,k;int row[m],column[n];memset(row,1,sizeof(row));memset(column,1,sizeof(column));for (i = 0; i < m; i++){for (j = 0; j < n; j++){if (matrix[i][j] == 0){row[i] = 0;column[j] = 0;}}}for (i = 0; i < m; i++){for (j = 0; j < n; j++){if (row[i] == 0 || column[j] == 0){matrix[i][j] = 0;}}}}


0 0
原创粉丝点击