ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 1743. Restore the Array From Adjacent Pairs

2022-07-19 12:31:44  阅读:252  来源: 互联网

标签:Restore Pairs nums int graph adjacentPairs Adjacent res array


原题链接在这里: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:

  • 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 has adjacentPairs as its pairs.

题解:

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有