其他分享
首页 > 其他分享> > LeetCode 1743. Restore the Array From Adjacent Pairs

LeetCode 1743. Restore the Array From Adjacent Pairs

作者:互联网

原题链接在这里:https://leetcode.com/problems/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:

题解:

These adjacent pairs are like edges. The first number in res array should have only one neighbor.

Build the graph, find the first number and perform DFS.

DFS state, graph, current number, current index, visited set and result array.

Time Complexity: O(e). n = number of nodes. e = adjacentPairs.length. Here n = e + 1.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int[] restoreArray(int[][] adjacentPairs) {
 3         Map<Integer, Set<Integer>> graph = new HashMap<>();
 4         for(int [] e : adjacentPairs){
 5             graph.putIfAbsent(e[0], new HashSet<Integer>());
 6             graph.putIfAbsent(e[1], new HashSet<Integer>());
 7             graph.get(e[0]).add(e[1]);
 8             graph.get(e[1]).add(e[0]);
 9         }
10         
11         int [] res = new int[graph.size()];
12         int head = 0;
13         for(Map.Entry<Integer, Set<Integer>> entry : graph.entrySet()){
14             if(entry.getValue().size() == 1){
15                 head = entry.getKey();
16                 break;
17             }
18         }
19         
20         dfs(graph, head, 0, new HashSet<Integer>(), res);
21         return res;
22     }
23     
24     private void dfs(Map<Integer, Set<Integer>> graph, int cur, int ind, Set<Integer> visited, int[] res){
25         res[ind] = cur;
26         visited.add(cur);
27         
28         Set<Integer> nexts = graph.get(cur);
29         for(int next : nexts){
30             if(visited.contains(next)){
31                 continue;
32             }
33             
34             dfs(graph, next, ind + 1, visited, res);
35         }
36     }
37 }

 

标签:Restore,Pairs,nums,int,graph,adjacentPairs,Adjacent,res,array
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16493643.html