-
Notifications
You must be signed in to change notification settings - Fork 3
/
1743.cpp
29 lines (29 loc) · 879 Bytes
/
1743.cpp
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
class Solution {
public:
vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {
unordered_map<int, vector<int>> relationship;
unordered_set<int> st;
for (auto& p : adjacentPairs) {
relationship[p[0]].push_back(p[1]);
relationship[p[1]].push_back(p[0]);
}
int current = INT_MIN;
for (auto& p : relationship) {
if (p.second.size() == 1) {
current = p.first;
break;
}
}
vector<int> res;
while (true) {
res.push_back(current);
st.insert(current);
for (auto& num : relationship[current]) {
if (!st.count(num)) current = num;
}
if (relationship[current].size() == 1) break;
}
res.push_back(current);
return res;
}
};