题目描述
存在一个由 n 个不同元素组成的整数数组 nums ,但你已经记不清具体内容。好在你还记得 nums 中的每一对相邻元素。
给你一个二维整数数组 adjacentPairs ,大小为 n - 1 ,其中每个 adjacentPairs[i] = [ui, vi] 表示元素 ui 和 vi 在 nums 中相邻。
题目数据保证所有由元素 nums[i] 和 nums[i+1] 组成的相邻元素对都存在于 adjacentPairs 中,存在形式可能是 [nums[i], nums[i+1]] ,也可能是 [nums[i+1], nums[i]] 。这些相邻元素对可以 按任意顺序 出现。
返回 原始数组 nums 。如果存在多种解答,返回 其中任意一个 即可。
示例 1:
输入:adjacentPairs = [[2,1],[3,4],[3,2]]输出:[1,2,3,4]解释:数组的所有相邻元素对都在 adjacentPairs 中。特别要注意的是,adjacentPairs[i] 只表示两个元素相邻,并不保证其 左-右 顺序。
示例 2:
输入:adjacentPairs = [[4,-2],[1,4],[-3,1]]输出:[-2,4,1,-3]解释:数组中可能存在负数。另一种解答是 [-3,1,4,-2] ,也会被视作正确答案。
示例 3:
输入:adjacentPairs = [[100000,-100000]]输出:[100000,-100000]
提示:
  • nums.length == n
  • adjacentPairs.length == n - 1
  • adjacentPairs[i].length == 2
  • 2 <= n <= 10^5
  • -10^5 <= nums[i], ui, vi <= 10^5
  • 题目数据保证存在一些以 adjacentPairs 作为元素对的数组 nums
方法一:哈希表
思路及算法
对于一维数组 nums 中的元素 nums[i],若其为数组的第一个或最后一个元素,则该元素有且仅有一个元素与其相邻;若其为数组的中间元素,则该元素有且仅有两个元素与其相邻。
我们可以对每个元素记录与它相邻的元素有哪些,然后依次检查每个元素的相邻元素数量,即可找到原数组的第一个元素和最后一个元素。由于我们可以返回任意一个满足条件的数组,故指定这两个元素中的一个为原数组的第一个元素,然后根据相邻元素信息确定数组的第二个、第三个元素……直到确定最后一个元素为止。
具体地,我们使用哈希表记录每一个的元素的相邻元素有哪些,然后我们遍历哈希表,找到有且仅有一个相邻元素的元素 e1作为原数组的第一个元素。那么与 e1唯一相邻的元素 e2 即为原数组的第二个元素。此时排除掉与 e2 相邻的 e1 后,可以确认与 e2相邻的 e3 即为原数组的第三个元素……以此类推,我们可以将原数组完整推断出来。
C++
classSolution {public:vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {unordered_map<int, vector<int>> mp;for (auto& adjacentPair : adjacentPairs) { mp[adjacentPair[0]].push_back(adjacentPair[1]); mp[adjacentPair[1]].push_back(adjacentPair[0]); }int n = adjacentPairs.size() + 1;vector<int> ret(n);for (auto& [e, adj] : mp) {if (adj.size() == 1) { ret[0] = e;break; } } ret[1] = mp[ret[0]][0];for (int i = 2; i < n; i++) {auto& adj = mp[ret[i - 1]]; ret[i] = ret[i - 2] == adj[0] ? adj[1] : adj[0]; }return ret; }};
Java
classSolution {publicint[] restoreArray(int[][] adjacentPairs) { Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();for (int[] adjacentPair : adjacentPairs) { map.putIfAbsent(adjacentPair[0], new ArrayList<Integer>()); map.putIfAbsent(adjacentPair[1], new ArrayList<Integer>()); map.get(adjacentPair[0]).add(adjacentPair[1]); map.get(adjacentPair[1]).add(adjacentPair[0]); }int n = adjacentPairs.length + 1;int[] ret = newint[n];for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {int e = entry.getKey(); List<Integer> adj = entry.getValue();if (adj.size() == 1) { ret[0] = e;break; } } ret[1] = map.get(ret[0]).get(0);for (int i = 2; i < n; i++) { List<Integer> adj = map.get(ret[i - 1]); ret[i] = ret[i - 2] == adj.get(0) ? adj.get(1) : adj.get(0); }return ret; }}
C#
publicclassSolution {publicint[] RestoreArray(int[][] adjacentPairs) { Dictionary<int, IList<int>> dictionary = new Dictionary<int, IList<int>>();foreach (int[] adjacentPair in adjacentPairs) {if (!dictionary.ContainsKey(adjacentPair[0])) { dictionary.Add(adjacentPair[0], new List<int>()); }if (!dictionary.ContainsKey(adjacentPair[1])) { dictionary.Add(adjacentPair[1], new List<int>()); } dictionary[adjacentPair[0]].Add(adjacentPair[1]); dictionary[adjacentPair[1]].Add(adjacentPair[0]); }int n = adjacentPairs.Length + 1;int[] ret = newint[n];foreach (KeyValuePair<int, IList<int>> pair in dictionary) {int e = pair.Key; IList<int> adj = pair.Value;if (adj.Count == 1) { ret[0] = e;break; } } ret[1] = dictionary[ret[0]][0];for (int i = 2; i < n; i++) { IList<int> adj = dictionary[ret[i - 1]]; ret[i] = ret[i - 2] == adj[0] ? adj[1] : adj[0]; }return ret; }}
Golang
funcrestoreArray(adjacentPairs [][]int) []int { n := len(adjacentPairs) + 1 g := make(map[int][]int, n)for _, p := range adjacentPairs { v, w := p[0], p[1] g[v] = append(g[v], w) g[w] = append(g[w], v) } ans := make([]int, n)for e, adj := range g {iflen(adj) == 1 { ans[0] = ebreak } } ans[1] = g[ans[0]][0]for i := 2; i < n; i++ { adj := g[ans[i-1]]if ans[i-2] == adj[0] { ans[i] = adj[1] } else { ans[i] = adj[0] } }return ans}
C
structHashTable {int key;int arr[2]; UT_hash_handle hh;};voidpush(struct HashTable** hashTable, int x, int y){structHashTable* tmp; HASH_FIND_INT(*hashTable, &x, tmp);if (tmp == NULL) { tmp = (struct HashTable*)malloc(sizeof(struct HashTable)); tmp->key = x, tmp->arr[0] = y, tmp->arr[1] = INT_MAX; HASH_ADD_INT(*hashTable, key, tmp); } else { tmp->arr[1] = y; }}struct HashTable* query(struct HashTable** hashTable, int x){structHashTable* tmp; HASH_FIND_INT(*hashTable, &x, tmp);return tmp;}int* restoreArray(int** adjacentPairs, int adjacentPairsSize, int* adjacentPairsColSize, int* returnSize){structHashTable* hashTable = NULL;for (int i = 0; i < adjacentPairsSize; i++) { push(&hashTable, adjacentPairs[i][0], adjacentPairs[i][1]); push(&hashTable, adjacentPairs[i][1], adjacentPairs[i][0]); }int n = adjacentPairsSize + 1;int* ret = (int*)malloc(sizeof(int) * n); *returnSize = n;structHashTable *iter, *tmp; HASH_ITER(hh, hashTable, iter, tmp) {if (iter->arr[1] == INT_MAX) { ret[0] = iter->key; } } ret[1] = query(&hashTable, ret[0])->arr[0];for (int i = 2; i < n; i++) {int* adj = query(&hashTable, ret[i - 1])->arr; ret[i] = ret[i - 2] == adj[0] ? adj[1] : adj[0]; }return ret;}
JavaScript
var restoreArray = function(adjacentPairs) {constmap = new Map();for (const adjacentPair of adjacentPairs) {map.get(adjacentPair[0]) ? map.get(adjacentPair[0]).push(adjacentPair[1]) : map.set(adjacentPair[0], [adjacentPair[1]]);map.get(adjacentPair[1]) ? map.get(adjacentPair[1]).push(adjacentPair[0]) : map.set(adjacentPair[1], [adjacentPair[0]]); }const n = adjacentPairs.length + 1;const ret = new Array(n).fill(0);for (const [e, adj] of map.entries()) {if (adj.length === 1) { ret[0] = e;break; } } ret[1] = map.get(ret[0])[0];for (let i = 2; i < n; i++) {const adj = map.get(ret[i - 1]); ret[i] = ret[i - 2] == adj[0] ? adj[1] : adj[0]; }return ret;};
复杂度分析
  • 时间复杂度:O(n)。其中 n 是原数组的长度。我们只需要遍历每一个元素一次。
  • 空间复杂度:O(n) 。其中 n 是原数组的长度。主要为哈希表的开销。
BY / 
本文作者:力扣
编辑&版式:霍霍
声明:本文归“力扣”版权所有,如需转载请联系。
点个在看,少个 bug👇
继续阅读
阅读原文