[LeetCode] 1743. Restore the Array From Adjacent Pairs
作者:互联网
There is an integer array nums
that consists of n
unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums
.
You are given a 2D integer array adjacentPairs
of size n - 1
where each adjacentPairs[i] = [ui, vi]
indicates that the elements ui
and vi
are adjacent in nums
.
It is guaranteed that every adjacent pair of elements nums[i]
and nums[i+1]
will exist in adjacentPairs
, either as [nums[i], nums[i+1]]
or [nums[i+1], nums[i]]
. The pairs can appear in any order.
Return the original array nums
. If there are multiple solutions, return any of them.
Example 1:
Input: adjacentPairs = [[2,1],[3,4],[3,2]] Output: [1,2,3,4] Explanation: This array has all its adjacent pairs in adjacentPairs. Notice that adjacentPairs[i] may not be in left-to-right order.
Example 2:
Input: adjacentPairs = [[4,-2],[1,4],[-3,1]] Output: [-2,4,1,-3] Explanation: There can be negative numbers. Another solution is [-3,1,4,-2], which would also be accepted.
Example 3:
Input: adjacentPairs = [[100000,-100000]] Output: [100000,-100000]
Constraints:
nums.length == n
adjacentPairs.length == n - 1
adjacentPairs[i].length == 2
2 <= n <= 105
-105 <= nums[i], ui, vi <= 105
- There exists some
nums
that hasadjacentPairs
as its pairs.
从相邻元素对还原数组。
存在一个由 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 。如果存在多种解答,返回 其中任意一个 即可。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这是一道图论的题。思路还是需要把图建立起来。因为是数组,原则上每个数字都有两个邻居,除了数组的头部和尾部的元素。所以我们的切入点可以从数组的头部/尾部元素。图建立好了之后,邻接矩阵的size为1的一定是头部/尾部元素。这样我们就可以开始把元素写入结果集了。因为题目说了input数组中每个元素都是不同的,所以对于每一个遍历到的元素,我额外用了一个hashset存储已经遍历过的数字,这样可以避免数字被重复加入结果集。
时间O(mn) - 一开始建图的时候复杂度就很高了
空间O(n)
Java实现
1 class Solution { 2 public int[] restoreArray(int[][] adjacentPairs) { 3 int n = adjacentPairs.length; 4 int[] res = new int[n + 1]; 5 HashMap<Integer, HashSet<Integer>> map = new HashMap<>(); 6 for (int[] pair : adjacentPairs) { 7 if (!map.containsKey(pair[0])) { 8 map.put(pair[0], new HashSet<>()); 9 } 10 map.get(pair[0]).add(pair[1]); 11 if (!map.containsKey(pair[1])) { 12 map.put(pair[1], new HashSet<>()); 13 } 14 map.get(pair[1]).add(pair[0]); 15 } 16 17 HashSet<Integer> result = new HashSet<>(); 18 int cur = Integer.MAX_VALUE; 19 int i = 0; 20 for (Map.Entry<Integer, HashSet<Integer>> e : map.entrySet()) { 21 // 说明是数组的头部 22 if (e.getValue().size() == 1) { 23 cur = e.getKey(); 24 break; 25 } 26 } 27 28 res[i] = cur; 29 i++; 30 result.add(cur); 31 while (i < res.length) { 32 for (int next : map.remove(cur)) { 33 if (next != cur && result.add(next)) { 34 res[i] = next; 35 i++; 36 cur = next; 37 } 38 } 39 } 40 return res; 41 } 42 }
标签:Restore,Pairs,nums,int,元素,map,adjacentPairs,Adjacent,pair 来源: https://www.cnblogs.com/cnoodle/p/14360081.html