-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
148 lines (132 loc) · 3.09 KB
/
main.cpp
File metadata and controls
148 lines (132 loc) · 3.09 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Source: https://leetcode.com/problems/set-matrix-zeroes
// Title: Set Matrix Zeroes
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an `m x n` `matrix`, return all elements of the `matrix` in spiral order.
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg
//
// ```
// Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
// Output: [1,2,3,6,9,8,7,4,5]
// ```
//
// **Example 2:**
// https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg
//
// ```
// Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
// Output: [1,2,3,4,8,12,11,10,9,5,6,7]
// ```
//
// **Constraints:**
//
// - `m == matrix.length`
// - `n == matrix[i].length`
// - `1 <= m, n <= 10`
// - `-100 <= matrix[i][j] <= 100`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <vector>
using namespace std;
// O(n) space
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<bool> rows(m);
vector<bool> cols(n);
// Check zeros
for (auto i = 0; i < m; i++) {
for (auto j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
rows[i] = true;
cols[j] = true;
}
}
}
// Update rows
for (auto i = 0; i < m; i++) {
if (rows[i]) {
for (auto j = 0; j < n; j++) {
matrix[i][j] = 0;
}
}
}
// Update columns
for (auto j = 0; j < n; j++) {
if (cols[j]) {
for (auto i = 0; i < m; i++) {
matrix[i][j] = 0;
}
}
}
}
};
// O(1) space
//
// We first check the first row and column.
// Then use the first row & column to store the zeros for other rows/columns.
class Solution2 {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
// Row 0
bool row0 = 1;
for (auto j = 0; j < n; j++) {
if (matrix[0][j] == 0) {
row0 = 0;
break;
}
}
// Column 0
bool col0 = 1;
for (auto i = 0; i < m; i++) {
if (matrix[i][0] == 0) {
col0 = 0;
break;
}
}
// Check zeros
for (auto i = 1; i < m; i++) {
for (auto j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
// Update rows
for (auto i = 1; i < m; i++) {
if (matrix[i][0] == 0) {
for (auto j = 1; j < n; j++) {
matrix[i][j] = 0;
}
}
}
// Update columns
for (auto j = 1; j < n; j++) {
if (matrix[0][j] == 0) {
for (auto i = 1; i < m; i++) {
matrix[i][j] = 0;
}
}
}
// Update row 0
if (row0 == 0) {
for (auto j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
// Update column 0
if (col0 == 0) {
for (auto i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
}
};