-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_7.cpp
130 lines (106 loc) · 2.74 KB
/
1_7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// 1_7.cpp
//
//
// Created by Pengyan Qin on 6/15/15.
//
//
#include <iostream>
using namespace std;
// time complexity: O(n^2)
// space complexity: O(2n)
void to_zero(int *matrix, int n){
int *row = new int[n]; // row[] containes undefined values
int *col = new int[n]; // col[] containes undefined values
for(int i = 0; i < n; ++i){
row[i] = 0;
col[i] = 0;
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(*(matrix + i*n + j) == 0){
row[i] = 1;
col[j] = 1;
}
}
}
for(int i = 0; i < n; ++i){
if(row[i]){
for(int j = 0; j < n; ++j)
*(matrix + i*n + j) = 0;
}
if(col[i]){
for(int j = 0; j < n; ++j)
*(matrix + j*n + i) = 0;
}
}
delete[] row;
delete[] col;
}
// time complexity: O(n^2)
// space complexity: O(1)
void to_zero1(int *matrix, int m, int n){ // m row, n column
bool row = false;
bool col = false;
for(int i = 0; i < n; ++i){ // check the first row
if(*(matrix + i) == 0)
row = true;
}
for(int i = 0; i < m; ++i){ // check the first column
if(*(matrix + i*n))
col = true;
}
for(int i = 1; i < m; ++i){ // check the rest of the matrix
for(int j = 1; j < n; ++j){
if(*(matrix + i*n +j) == 0){
*(matrix + i*n) = 0;
*(matrix + j) = 0;
}
}
}
for(int i = 0; i < n; ++i){ // set zero for each column
if(*(matrix + i) == 0){
for(int j = 0; j < m; ++j)
*(matrix + j*n + i) = 0;
}
}
for(int i = 0; i < m; ++i){ // set zero for each row
if(*(matrix + i*n) == 0){
for(int j = 0; j < n; ++j)
*(matrix + i*n +j) = 0;
}
}
if(row){ // set zero for first row
for(int i = 0; i < n; ++i)
*(matrix + i) = 0;
}
if(col){ // set zero for first column
for(int i = 0; i < m; ++i)
*(matrix + i*n) = 0;
}
return;
}
int main(){
int a[5][4] = {{1, 6, 0, 1},
{2, 9, 3, 2},
{0, 3, 6, 3},
{5, 1, 0, 8},
{3, 5, 1, 5}};
int m = 5;
int n = 4;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
cout << a[i][j] << " ";
}
cout << endl;
}
to_zero1((int*)a, m, n);
cout << "to_zero result: " << endl;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}