Skip to content

Commit 9c226b7

Browse files
authored
Create 322-reconstruct-itinerary.js
1 parent b2d5668 commit 9c226b7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

322-reconstruct-itinerary.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string[][]} tickets
3+
* @return {string[]}
4+
*/
5+
6+
const findItinerary = tickets => {
7+
let db = {};
8+
let result = [];
9+
tickets.forEach(node => {
10+
if (db[node[0]]) {
11+
db[node[0]].push(node[1]);
12+
} else {
13+
db[node[0]] = [node[1]];
14+
}
15+
})
16+
17+
for(let prop in db){
18+
db[prop].sort();
19+
}
20+
21+
const dfs = (from) => {
22+
while (db[from] && db[from].length > 0) {
23+
dfs(db[from].shift());
24+
}
25+
result.unshift(from);
26+
}
27+
28+
dfs('JFK');
29+
return result;
30+
};
31+

0 commit comments

Comments
 (0)