Skip to content

Commit 7488df6

Browse files
author
lightmen
committed
add set-matrix-zeroes.c
1 parent 6e55646 commit 7488df6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

set-matrix-zeroes.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
void setZeroes(int** matrix, int m, int n) {
2+
int *row,*col;
3+
int i,j;
4+
row = (int *)malloc(sizeof(int) * m);
5+
col = (int *)malloc(sizeof(int) * n);
6+
7+
memset(row,0,sizeof(int) * m);
8+
memset(col,0,sizeof(int) * n);
9+
for(i = 0; i < m; ++i){
10+
for(j = 0; j < n; ++j){
11+
if(!matrix[i][j]){
12+
row[i] = 1;
13+
col[j] = 1;
14+
}
15+
}
16+
}
17+
18+
for(i = 0; i < m; ++i)
19+
if(row[i]){
20+
for(j = 0; j < n; ++j)
21+
matrix[i][j] = 0;
22+
}
23+
for(j = 0; j < n; ++j)
24+
if(col[j]){
25+
for(i = 0; i < m; ++i)
26+
matrix[i][j] = 0;
27+
}
28+
}

0 commit comments

Comments
 (0)