[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 ...
随机推荐
- 记安装Wampsever
遇到的问题: Wampsever 启动所有服务后图标为黄色 localhost 问题:显示 IIS Windows 在用 localhost 访问本机的php文件和用ip地址(不是127.0.0.1) ...
- this大全,还有谁??????!!!!!!!
this在函数调用时创建,一般的对象没有this,全局window可以理解为一个函数,他有一个全局this JavaScript 语言之所以有this的设计,跟内存里面的数据结构有关系. 函数里thi ...
- 第十五章 使用PyQt进行Python图形界面程序开发
在基础知识部分的最后一章<第十三章 Python基础篇结束章>的<第13.3节 图形界面开发tkinter>简单介绍了Python内置图形界面标准库tkinter,当时特别强调 ...
- 开源版本Visifire的应用
Visifire曾经开源,保持使用开源版本是不会有版权问题滴. 引用的命名控件 using Visifire.Charts; using Visifire.Commons; 一.应用示例主要代码 // ...
- 原生JS学习之秒表、日历
Tips:涉及知识点:Date setInterval DOM 秒表 效果图: 简单构造出草图 Html代码 1 <!DOCTYPE html> 2 <html> ...
- jsonp使用post方法
来源https://www.jb51.net/article/68980.htm
- 手把手教你写DI_0_DI是什么?
DI是什么? Dependency Injection 常常简称为:DI. 它是实现控制反转(Inversion of Control – IoC)的一个模式. fowler 大大大神 "几 ...
- Apriori 算法-如何进行关联规则挖掘
公号:码农充电站pro 主页:https://codeshellme.github.io 在数据分析领域有一个经典的故事,叫做"尿布与啤酒". 据说,在美国西部的一家连锁超市发现, ...
- 使用.Net Core做个爬虫
最近接手一个新项目,爬亚马逊分类.商品数据.记得大学的时候,自己瞎玩,写过一个爬有缘网数据的程序,那个时候没有考虑那么多,写的还是单线程,因为网站没有反爬,就不停的一直请求,记得放到实验室电脑上一天, ...
- 题解-洛谷P4229 某位歌姬的故事
题面 洛谷P4229 某位歌姬的故事 \(T\) 组测试数据.有 \(n\) 个音节,每个音节 \(h_i\in[1,A]\),还有 \(m\) 个限制 \((l_i,r_i,g_i)\) 表示 \( ...