-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.js
36 lines (34 loc) · 769 Bytes
/
10.js
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
var restoreArray = function(adjacentPairs) {
const graph =new Map();
for (const [u,v] of adjacentPairs){
if (!graph.has(u)) {
graph.set(u,[]);
}
if (!graph.has(v)) {
graph.set(v,[]);
}
graph.get(u).push(v);
graph.get(v).push(u);
}
let cur=null;
for (const u of graph.keys()){
if (graph.get(u).length===1){
cur=u;
break;
}
}
const ans=[];
const seen= new Set();
while (cur !== null){
ans.push(cur);
seen.add(cur);
const neis=graph.get(cur);
cur=null;
for (let nei of neis){
if (!seen.has(nei)){
cur=nei;
}
}
}
return ans
};