Skip to content

Commit 21edaa3

Browse files
authored
Merge pull request #3668 from drxlx/0332-reconstruct-itinerary
Create 0332-reconstruct-itinerary.swift
2 parents 272e0c6 + bab15f8 commit 21edaa3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: swift/0332-reconstruct-itinerary.swift

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func findItinerary(_ tickets: [[String]]) -> [String] {
3+
var adj: [String: [String]] = [:]
4+
for t in tickets {
5+
let (src, dst) = (t[0], t[1])
6+
adj[src, default: []].append(dst)
7+
}
8+
9+
for src in adj.keys {
10+
adj[src]?.sort { $0 > $1 }
11+
}
12+
13+
var res = [String]()
14+
func dfs(_ src: String) {
15+
while let dst = adj[src]?.popLast() {
16+
dfs(dst)
17+
}
18+
res.append(src)
19+
}
20+
21+
dfs("JFK")
22+
return res.reversed()
23+
}
24+
}

0 commit comments

Comments
 (0)