leetCode解题报告之Clone Graph

怼烎@ 2022-08-26 04:54 204阅读 0赞

题目:

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

OJ’s undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph { 0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

  1. 1
  2. / \
  3. / \
  4. 0 --- 2
  5. / \
  6. \_/

分析:

这个题目提供给我们一个已知的无向图,图上结点都有自己的值(label)还有和其他图结点的关系(包括和其他结点的连接关系,还有可能自己关联自己,形成环),要求我们要对这个已知图做一个“克隆”(有点类似于上次那题copy list的题目)

图上结点的数据结构:

class UndirectedGraphNode {

int label;

ArrayList neighbors;

UndirectedGraphNode(int x) {

label = x;

neighbors = new ArrayList();

}

};

解题思路:

首先,我们会想到,如果给你一个图上的结点,比如 结点0,它和结点 1 还有 结点 2 都有关联,如果我们要设置 结点0 的关联关系的List集合的话,如果结点 1 和结点 2已经存在的话,那么我们将不能再创建一遍已经存在的结点。

综上,

  1. 我们会用一个HashMap map, 来存储已经被我们new 出来的图上的结点

  2. 为了能够把图上的所有结点都顺利的创建出来并且把它们的关联关系也确定好,我们需要有一个 队列, 先把 提供给我们的图上的一个结点放入 queue中,然后循环,取出队列中的“头结点”(tempNode),如果结点有和其他结点的关系List, 遍历关系结合List中的结点,如果结点不包括在map中,那么证明这个结点还没有被创建,此时我们copy一个该图结点,然后 map.put(OldNode, NewNode); 并把这个结点加入到新的结点的关系集合List中.

  3. 为了避免“有环”(自己连接到自己的结点)的情况会一直重复,我们需要用一个 Set 集合来存下所有处理过的 图结点!

【讲得有点乱,具体看代码吧!】

AC代码:

  1. package cn.xym.leetcode.copygraph;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.LinkedList;
  6. import java.util.Queue;
  7. import java.util.Set;
  8. class UndirectedGraphNode {
  9. int label;
  10. ArrayList<UndirectedGraphNode> neighbors;
  11. UndirectedGraphNode(int x) {
  12. label = x;
  13. neighbors = new ArrayList<UndirectedGraphNode>();
  14. }
  15. };
  16. public class Solution {
  17. public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
  18. if (node == null)
  19. return null;
  20. //存已经copy的结点, 存入的entry的key为 旧图的node,
  21. //value为新图的node("copy旧图node的结点").
  22. HashMap<UndirectedGraphNode,UndirectedGraphNode> map
  23. = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
  24. map.put(node, new UndirectedGraphNode(node.label)); //初始化,先copy 第一个结点 node
  25. Set<UndirectedGraphNode> visitedSet
  26. = new HashSet<UndirectedGraphNode>(); //存放被处理过的图结点
  27. Queue<UndirectedGraphNode> queue
  28. = new LinkedList<UndirectedGraphNode>(); //存放待处理的图结点
  29. queue.add(node); //往队列中加入一个结点node
  30. while (!queue.isEmpty()){
  31. //取出队头结点
  32. UndirectedGraphNode tempNode = queue.remove();
  33. //判断这个结点是否已经被处理过了(防止“环”)
  34. if (visitedSet.contains(tempNode))
  35. continue;
  36. visitedSet.add(tempNode); //加入处理过结点的集合
  37. /*处理当前结点的关系集合List*/
  38. for (UndirectedGraphNode i : tempNode.neighbors){
  39. if (!map.containsKey(i)){
  40. UndirectedGraphNode copy = new UndirectedGraphNode(i.label);
  41. map.put(i, copy);
  42. map.get(tempNode).neighbors.add(copy);
  43. }else{
  44. map.get(tempNode).neighbors.add(map.get(i));
  45. }
  46. queue.add(i);
  47. }
  48. }
  49. return map.get(node);
  50. }
  51. }

发表评论

表情:
评论列表 (有 0 条评论,204人围观)

还没有评论,来说两句吧...

相关阅读