克隆图记住:一个map一个queue,照葫芦画瓢BFS

找到一个节点就画一个对应的新的,用map对应,然后添加邻居,记录邻居到queue

public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node==null) return node;
UndirectedGraphNode res = new UndirectedGraphNode(node.label);
Queue<UndirectedGraphNode> queue = new LinkedList<>();
Map<UndirectedGraphNode,UndirectedGraphNode> map = new HashMap<>();
queue.offer(node);
map.put(node,res);
while (!queue.isEmpty()){
UndirectedGraphNode cur = queue.poll();
List<UndirectedGraphNode> neighbors = cur.neighbors;
for (UndirectedGraphNode neighbor :
neighbors) {
if (!map.containsKey(neighbor)){
UndirectedGraphNode newNode = new UndirectedGraphNode(neighbor.label);
queue.offer(neighbor);
//在旧图中每遍历到一个节点,就在map中给新图申请一个对应的内存
map.put(neighbor,newNode);
}
//根据旧图中的位置为新图添加邻居
map.get(cur).neighbors.add(map.get(neighbor));
}
}
return res;
}

[leetcode]Q133Clone Graph的更多相关文章

  1. [LeetCode] Clone Graph 无向图的复制

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

  2. LeetCode OJ-- Clone Graph **@

    https://oj.leetcode.com/problems/clone-graph/ 图的拷贝,就是给一个图,再弄出一个一模一样的来. /** * Definition for undirect ...

  3. [LeetCode] Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  4. [LeetCode] Clone Graph 克隆无向图

    Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...

  5. [leetcode]Clone Graph @ Python

    原题地址:https://oj.leetcode.com/problems/clone-graph/ 题意:实现对一个图的深拷贝. 解题思路:由于遍历一个图有两种方式:bfs和dfs.所以深拷贝一个图 ...

  6. [LeetCode] 261. Graph Valid Tree 图是否是树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  7. LeetCode:Clone Graph

    题目如下:实现克隆图的算法  题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...

  8. [LeetCode#261] Graph Valid Tree

    Problem: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair o ...

  9. LeetCode - Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

随机推荐

  1. java集合源码分析(三):ArrayList

    概述 在前文:java集合源码分析(二):List与AbstractList 和 java集合源码分析(一):Collection 与 AbstractCollection 中,我们大致了解了从 Co ...

  2. JZOJ2020年9月12日提高B组反思

    CSP第1轮倒计时:29天 JZOJ2020年9月12日提高B组反思 T1 放在T1却是最难的一题 明显需要高精度 但是我小学奥数没学好,不知道怎么把正有理数转化成分数 T2 明显的DP 可惜的是我文 ...

  3. PyQt(Python+Qt)学习随笔:QListWidgetItem的重要方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListWidgetItem类为QListWidget类提供构成QListWidget列表部件的项 ...

  4. 结对项目Myapp

    ·Github地址:https://github.com/Dioikawa/Myapp ·成员:陈杰才(3118005089) 蔡越(3118005086) ·耗费时间估计: PSP2.1 Perso ...

  5. OpenResty&Canal

    OpenResty&Canal OpenResty 提供缓存功能 封装了Nginx,并且提供了Lua扩展,大大提升了Nginx的并发处理能力10k~1000k Nginx限流 1.控制速率 2 ...

  6. 20201207-2 openpyxl 库与模块导入

    1-1 import openpyxl # 通过文件路径,打开工作簿 wb1 = openpyxl.load_workbook('./demo_excel.xlsx') # 用 Workbook() ...

  7. Day5 - 06 函数的参数-命名关键字参数

    引子:对于关键字参数,调用时可以传入任意个不受限制的关键字参数,至于到底传入了哪些,就需要在函数内部通过[函数里定义的关键字参数]检查,例子里就是通过otherinfo检查.        >& ...

  8. MySQL全备及备份文件删除脚本

    1.数据库全备 #!/bin/shv_user="root"v_password="mysql"backup_date=`date +%Y%m%d%H%M` M ...

  9. JavaSE10-继承&super&this&抽象类

    1.继承 1.1 概述 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那一个类即可. 其中,多个类可以称为子类,单独那一个类称为父类.超类(s ...

  10. IDEA中flink程序报错找不到类

    Idea中运行flink程序,报错找不到类,其中pom文件中一项依赖为: <dependency> <groupId>org.apache.flink</groupId& ...