-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path133-clone-graph.swift
44 lines (42 loc) · 1.27 KB
/
133-clone-graph.swift
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
/**
* Definition for a Node.
* public class Node {
* public var val: Int
* public var neighbors: [Node?]
* public init(_ val: Int) {
* self.val = val
* self.neighbors = []
* }
* }
*/
class Solution {
// Time O(n)
// Space O(n)
func cloneGraph(_ node: Node?) -> Node? {
guard let node = node else { return nil }
var copies: [Node?] = Array(repeating: nil, count: 101)
var stack = [node]
var head: Node = makeCopy(node.val, &copies)
while !stack.isEmpty {
let top = stack.removeLast()
let copy = makeCopy(top.val, &copies)
guard copy.neighbors.count != top.neighbors.count else { continue }
for neighbor in top.neighbors {
guard let neighbor = neighbor else {
copy.neighbors.append(nil)
continue
}
let neighborCopy = makeCopy(neighbor.val, &copies)
copy.neighbors.append(neighborCopy)
stack.append(neighbor)
}
}
return head
}
func makeCopy(_ val: Int, _ copies: inout [Node?]) -> Node {
if copies[val] == nil {
copies[val] = Node(val)
}
return copies[val]!
}
}