-
Notifications
You must be signed in to change notification settings - Fork 1
/
1743.从相邻元素对还原数组.cpp
60 lines (55 loc) · 1.7 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* @lc app=leetcode.cn id=1743 lang=cpp
*
* [1743] 从相邻元素对还原数组
*/
#include <iostream>
#include <vector>
using namespace std;
// @lc code=start
class Solution {
public:
vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {
vector<int> res = adjacentPairs[0];
int start = 1;
while (start < adjacentPairs.size()) {
for (int i = start; i < adjacentPairs.size(); i++) {
// 头部 , 左边
if (res[0] == adjacentPairs[i][0]) {
res.insert(res.begin(), adjacentPairs[i][1]);
adjacentPairs[i] = adjacentPairs[start];
start++;
break;
}
if (res[0] == adjacentPairs[i][1]) {
res.insert(res.begin(), adjacentPairs[i][0]);
adjacentPairs[i] = adjacentPairs[start];
start++;
break;
}
if (res.back() == adjacentPairs[i][0]) {
res.push_back(adjacentPairs[i][1]);
adjacentPairs[i] = adjacentPairs[start];
start++;
break;
}
if (res.back() == adjacentPairs[i][1]) {
res.push_back(adjacentPairs[i][0]);
adjacentPairs[i] = adjacentPairs[start];
start++;
break;
}
}
}
return res;
}
};
// @lc code=end
int main() {
Solution s;
vector<vector<int>> input = { {100000,-100000} };
vector <int> res = s.restoreArray(input);
for (auto i : res) {
cout << i << ' ';
}
}