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