-
Notifications
You must be signed in to change notification settings - Fork 160
/
set-matrix-zeroes.md
43 lines (37 loc) · 979 Bytes
/
set-matrix-zeroes.md
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
<p>Given a <em>m</em> x <em>n</em> matrix, if an element is 0, set its entire row and column to 0. Do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a>.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong>
[
[1,1,1],
[1,0,1],
[1,1,1]
]
<strong>Output:</strong>
[
[1,0,1],
[0,0,0],
[1,0,1]
]
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong>
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
<strong>Output:</strong>
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
</pre>
<p><strong>Follow up:</strong></p>
<ul>
<li>A straight forward solution using O(<em>m</em><em>n</em>) space is probably a bad idea.</li>
<li>A simple improvement uses O(<em>m</em> + <em>n</em>) space, but still not the best solution.</li>
<li>Could you devise a constant space solution?</li>
</ul>