[leetcode]Q133Clone Graph
克隆图记住:一个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的更多相关文章
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- LeetCode OJ-- Clone Graph **@
https://oj.leetcode.com/problems/clone-graph/ 图的拷贝,就是给一个图,再弄出一个一模一样的来. /** * Definition for undirect ...
- [LeetCode] Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [LeetCode] Clone Graph 克隆无向图
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...
- [leetcode]Clone Graph @ Python
原题地址:https://oj.leetcode.com/problems/clone-graph/ 题意:实现对一个图的深拷贝. 解题思路:由于遍历一个图有两种方式:bfs和dfs.所以深拷贝一个图 ...
- [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), ...
- LeetCode:Clone Graph
题目如下:实现克隆图的算法 题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...
- [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 ...
- LeetCode - Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
随机推荐
- Verilog 分频器
verilog设计进阶 时间:2014年5月6日星期二 主要收获: 1. 自己动手写了第一个verilog程序. 题目: 利用10M的时钟,设计一个单周期形状如下的周期波形. 思考: 最开始的想法是: ...
- Alpha冲刺——代码规范,冲刺计划
这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2/ 这个作业要求在哪里 https://edu.cnblogs.com/campus/f ...
- 一种更优雅的Flutter Dialog解决方案
前言 系统自带的Dialog实际上就是Push了一个新页面,这样存在很多好处,但是也存在一些很难解决的问题 必须传BuildContext loading弹窗一般都封装在网络框架中,多传个contex ...
- nodejs事件和事件循环简介
目录 简介 事件 事件循环 事件循环的阻塞 事件循环举例 栈和消息队列 作业队列和promise process.nextTick() setImmediate() setInterval() 简介 ...
- Hbase 2.2.2 安装、配置(兼容 Hadoop 3.1.3)
准备 Hbase 2.2.2 安装包 下载链接 链接:https://pan.baidu.com/s/1TqEry-T7sYpq4PdhgLWdcQ 提取码:de5z 安装 上传到虚拟机上,之后解压即 ...
- Docker 本地镜像推送到阿里云(五)
最近在学习Docker,从安装Docker开始,到自定义制作镜像mycentos,但是怎么把它推送到阿里云,一直是困扰我的问题.后面有时间了解了一下,根据本地镜像推动到阿里云其实并没有我们想象中那么难 ...
- 图 Graph
本文主要内容为:图的定义以及基本术语 图的定义 图 图G的组成:由 数据元素的集合E 和 数据间的关系集合E 组成,记作:G = <V, E> 顶点 (vertex):数据元素,V就是顶点 ...
- 第四篇 Scrum 冲刺博客
一.站立式会议 1. 会议照片 2. 工作汇报 团队成员名称 昨日(25日)完成的工作 今天(26日)计划完成的工作 工作中遇到的困难 陈锐基 - 完善表白墙动态的全局状态管理 - 完成发布页面的布局 ...
- Day6 Scrum 冲刺博客
一.站立式会议# 1. 会议照片 2. 工作进度+燃尽图 团队成员 昨日完成工作 今日工作计划 遇到的困难 周梓波 将方块旋转变形 添加键盘监听事件 不熟悉监听事件的操作 纪昂学 左右 ...
- Ubuntu 16.04 系统基础开发环境搭建
1.安装 Git sudo apt-get update sudo apt-get install git Do you want to continue? [Y/n] Y git --version ...