Class Solution
- java.lang.Object
-
- g1701_1800.s1743_restore_the_array_from_adjacent_pairs.Solution
-
public class Solution extends Object
1743 - Restore the Array From Adjacent Pairs.Medium
There is an integer array
numsthat consists ofnunique elements, but you have forgotten it. However, you do remember every pair of adjacent elements innums.You are given a 2D integer array
adjacentPairsof sizen - 1where eachadjacentPairs[i] = [ui, vi]indicates that the elementsuiandviare adjacent innums.It is guaranteed that every adjacent pair of elements
nums[i]andnums[i+1]will exist inadjacentPairs, 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 == nadjacentPairs.length == n - 1adjacentPairs[i].length == 22 <= n <= 105-105 <= nums[i], ui, vi <= 105- There exists some
numsthat hasadjacentPairsas its pairs.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]restoreArray(int[][] adjacentPairs)
-