-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1743.从相邻元素对还原数组.js
45 lines (39 loc) · 936 Bytes
/
1743.从相邻元素对还原数组.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
37
38
39
40
41
42
43
44
45
/*
* @lc app=leetcode.cn id=1743 lang=javascript
*
* [1743] 从相邻元素对还原数组
*/
// @lc code=start
/**
* @param {number[][]} adjacentPairs
* @return {number[]}
*/
var restoreArray = function (adjacentPairs) {
const map = adjacentPairs.reduce((pre, item) => {
const [x, y] = item;
pre[x] = (pre[x] || []).concat(y);
pre[y] = (pre[y] || []).concat(x);
return pre;
}, {});
let stack = [];
for (let key in map) {
if (map[key].length === 1) {
stack = [+key];
break;
}
}
let res = [];
const cache = {};
while (stack.length) {
const top = stack.shift();
res.push(top);
cache[top] = true;
const next = map[top].filter(i => !cache[i]);
if (next.length) {
stack.unshift(next[0]);
}
}
return res;
};
// @lc code=end
restoreArray([[2,1],[3,4],[3,2]])