克隆图记住:一个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. LeetCode 044 Wildcard Matching

    题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...

  2. 20200203_windows2012下安装mysql 5.7.29

    一.   检查系统版本: 二. 下载mysql, 下载地址: https://dev.mysql.com/downloads/mysql/5.7.html#downloads 三.   解压下载后的压 ...

  3. 如何使用 K8s 两大利器"审计"和"事件"帮你摆脱运维困境?

    概述 下面几个问题,相信广大 K8s 用户在日常集群运维中都曾经遇到过: 集群中的某个应用被删除了,谁干的? Apiserver 的负载突然变高,大量访问失败,集群中到底发生了什么? 集群节点 Not ...

  4. RedHat-Linux操作指令第1篇

    不同的linux系统切换方式会稍有一点差别 从图形界面切换到字符界面:Alt+F(1-8) 或者 Alt+Ctrl+Shift+F(1-8) 从字符界面切换回图形界面:Alt+F7 字符界面启动到图形 ...

  5. BUUOJ 杂项MISC(1)

    爱因斯坦 下载之后解压打开是一张爱因斯坦的图片,看来是图片隐写题 使用binwalk -e misc2.jpg 获得一个有flag.txt的压缩包,但是需要密码才能打开,猜想密码在图片里面,把图片丢进 ...

  6. Java并发编程的艺术(九)——闭锁、同步屏障和信号量

    闭锁:CountDownLatch 使用场景 当前线程需要等待若干条线程执行完毕后,才能继续执行的情况. 也可以是若干个步骤执行完毕后的情况. 使用方法 初始化闭锁的时候,填入计数值,然后等待其他线程 ...

  7. sql server的bcp指令

    有时需要允许bcp指令 -- 允许配置高级选项EXEC sp_configure 'show advanced options', 1GO-- 重新配置RECONFIGUREGO-- 启用xp_cmd ...

  8. java web简单的对数据库存数据

    1.建立一个表,分别有Coursename,teachername,Place,Id;这些数据跟sql语句中的相对应 2.在src包目录下创建这些类 3.在WebContent目录下创建jsp,Mai ...

  9. feign代码自动生成插件

    简介 feign对微服务之间的http调用做了一层封装,如果B项目想调用A项目的一个web服务,只需要编写对应的接口并标注FeignClient注解.但如果接口发生了变更,对应的Feign代码往往会忘 ...

  10. 2020-2021-1 20209307《Linux内核原理与分析》第六周作业

    这个作业属于哪个课程 <2020-2021-1Linux内核原理与分析)> 这个作业要求在哪里 <2020-2021-1Linux内核原理与分析第六周作业> 这个作业的目标 & ...