-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution73.swift
More file actions
79 lines (74 loc) · 1.96 KB
/
Solution73.swift
File metadata and controls
79 lines (74 loc) · 1.96 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
//
// Solution73.swift
// leetcode
//
// Created by peter on 2020/7/3.
// Copyright © 2020 peter. All rights reserved.
//
import Foundation
class Solution73 {
func setZeroes(_ matrix: inout [[Int]]) {
var rows : Set<Int> = []
var columns : Set<Int> = []
for i in 0 ..< matrix.count {
for j in 0 ..< matrix[i].count {
if matrix[i][j] == 0 {
rows.insert(i)
columns.insert(j)
}
}
}
for i in rows {
for j in 0 ..< matrix[i].count {
matrix[i][j] = 0
}
}
for j in columns {
for i in 0 ..< matrix.count {
matrix[i][j] = 0
}
}
}
func setZeroesH(_ matrix: inout [[Int]]) {
var zeroRow = false
var zeroColumn = false
for i in 0 ..< matrix.count {
for j in 0 ..< matrix[i].count {
if matrix[i][j] == 0 {
if i == 0 {
zeroRow = true
}
if j == 0 {
zeroColumn = true
}
matrix[0][j] = 0
matrix[i][0] = 0
}
}
}
for j in 0 ..< matrix[0].count {
if matrix[0][j] == 0 && j != 0 {
for i in 0 ..< matrix.count {
matrix[i][j] = 0
}
}
}
for i in 0 ..< matrix.count {
if matrix[i][0] == 0 && i != 0 {
for j in 0 ..< matrix[i].count {
matrix[i][j] = 0
}
}
}
if zeroRow {
for j in 0 ..< matrix[0].count {
matrix[0][j] = 0
}
}
if zeroColumn {
for i in 0 ..< matrix.count {
matrix[i][0] = 0
}
}
}
}