File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func setZeroes( _ matrix: inout [ [ Int ] ] ) {
3
+ var firstRowHasZero = false
4
+ var firstColHasZero = false
5
+
6
+ for col in 0 ..< matrix [ 0 ] . count {
7
+ if matrix [ 0 ] [ col] == 0 {
8
+ firstRowHasZero = true
9
+ break
10
+ }
11
+ }
12
+
13
+ for row in 0 ..< matrix. count {
14
+ if matrix [ row] [ 0 ] == 0 {
15
+ firstColHasZero = true
16
+ break
17
+ }
18
+ }
19
+
20
+ // marking
21
+ for row in 0 ..< matrix. count {
22
+ for col in 0 ..< matrix [ row] . count {
23
+ if matrix [ row] [ col] == 0 {
24
+ matrix [ row] [ 0 ] = 0
25
+ matrix [ 0 ] [ col] = 0
26
+ }
27
+ }
28
+ }
29
+
30
+ // row
31
+ for row in 1 ..< matrix. count {
32
+ if matrix [ row] [ 0 ] == 0 {
33
+ for col in 0 ..< matrix [ row] . count {
34
+ matrix [ row] [ col] = 0
35
+ }
36
+ }
37
+ }
38
+
39
+ // column
40
+ for col in 1 ..< matrix [ 0 ] . count {
41
+ if matrix [ 0 ] [ col] == 0 {
42
+ for row in 0 ..< matrix. count {
43
+ matrix [ row] [ col] = 0
44
+ }
45
+ }
46
+ }
47
+
48
+ if firstRowHasZero {
49
+ for col in 0 ..< matrix [ 0 ] . count {
50
+ matrix [ 0 ] [ col] = 0
51
+ }
52
+ }
53
+
54
+ if firstColHasZero {
55
+ for row in 0 ..< matrix. count {
56
+ matrix [ row] [ 0 ] = 0
57
+ }
58
+ }
59
+ }
60
+ }
61
+
You can’t perform that action at this time.
0 commit comments