[Algo] 132. Deep Copy Undirected Graph
Make a deep copy of an undirected graph, there could be cycles in the original graph.
Assumptions
- The given graph is not null
DFS
/*
* class GraphNode {
* public int key;
* public List<GraphNode> neighbors;
* public GraphNode(int key) {
* this.key = key;
* this.neighbors = new ArrayList<GraphNode>();
* }
* }
*/
public class Solution {
public List<GraphNode> copy(List<GraphNode> graph) {
// Write your solution here.
Map<GraphNode, GraphNode> map = new HashMap<>();
for (GraphNode node : graph) {
if (!map.containsKey(node)) {
map.put(node, new GraphNode(node.key));
}
dfs(node, map);
}
return new ArrayList<>(map.values());
} private void dfs(GraphNode node, Map<GraphNode, GraphNode> map) {
GraphNode newNode = map.get(node);
for (GraphNode gNode : node.neighbors) {
if (!map.containsKey(gNode)) {
map.put(gNode, new GraphNode(gNode.key));
}
newNode.neighbors.add(map.get(gNode));
}
}
}
BFS
/*
* class GraphNode {
* public int key;
* public List<GraphNode> neighbors;
* public GraphNode(int key) {
* this.key = key;
* this.neighbors = new ArrayList<GraphNode>();
* }
* }
*/
public class Solution {
public List<GraphNode> copy(List<GraphNode> graph) {
// Write your solution here.
Map<GraphNode, GraphNode> map = new HashMap<>();
for (GraphNode gNode: graph) {
map.put(gNode, new GraphNode(gNode.key));
}
for (GraphNode node : graph) {
GraphNode cpNode = map.get(node);
for (GraphNode nei: node.neighbors) {
cpNode.neighbors.add(map.get(nei));
}
}
return new ArrayList<>(map.values());
}
}
[Algo] 132. Deep Copy Undirected Graph的更多相关文章
- [Algo] 131. Deep Copy Linked List With Random Pointer
Each of the nodes in the linked list has another pointer pointing to a random node in the list or nu ...
- Shallow copy and Deep copy
Shallow copy and Deep copy 第一部分: 一.来自wikipidia的解释: Shallow copy One method of copying an object is t ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- C# Bitmap deep copy
今天在研究一个关于 Bitmap deep copy 的问题, 经过一系列的查询,在StackOverFlow上面找到了答案,遂记录下来: public static Bitmap DeepCopyB ...
- shallow copy 和 deep copy 的示例
本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5481874.html (Robin) Student package base; impo ...
- python deep copy and shallow copy
Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings ...
- Deep Copy cv::StereoBM 深度拷贝
在使用OpenCV的三维立体重建的库时,一个重要的步骤就是生成左右视图的差异图Disparity,而控制生成disparity的参数的类是cv::StereoBM,我们有时候需要拷贝一份cv::Ste ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
随机推荐
- react的this.setState详细介绍
this.setState是react类组件中最常用的一个react API,使用它可以改变state从而改变页面.今天我们就来详细的学习一下这个东西.比如: import React, { Comp ...
- 九十九、SAP中ALV事件之十二,给ALV的标题栏添加图片
一.在OAER中找一个喜欢的图片,对象标识为“TRVPICTURE04” 二.来到我们的代码区,输入代码 三.效果如下 很完美
- 五十四、SAP中LVC表格每列的宽度自适应
一.之前我们的LVC表格输出的界面,有些列太宽余留空白区块太多,有些列则显示不全还带省略号等 二.我们来到'REUSE_ALV_GRID_DISPLAY_LVC'的模块中,查看他的属性 三.我们查看L ...
- office组件导入导出常见异常记录
异常:未能加载文件或程序集"Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken ...
- Sublime 一些常用快捷键
Sublime插件安装和使用-----------------------------最常用的1.新建文件-输入"html:xt"后 按"Ctrl+E键"或 & ...
- python连接 ssh
import paramiko # private = paramiko.RSAKey.from_private_key() 秘钥 trans = paramiko.Transport((" ...
- Vuex基本介绍
1.什么是Vuex Vuex是一个专为vue.js应用程序开发的状态管理模式. 状态管理:data里面的变量都是vue的状态. 2.为什么要用Vuex 当我们构建一个中大型的单页面应用程序时,Vuex ...
- C++ 设置自动启动
WCHAR pFileName[MAX_PATH] = {}; //得到程序自身的全路径 DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PA ...
- EUI库 - 9 - 数据集合 - 列表
List 和DataGroup的区别 1 选中一项 会触发 eui.ItemEvent.ITEM_TAP 事件, 2 有选中项的概念,可以设置 List 里的默认选中项 selectedIn ...
- 21 ~ express ~ 前台内容分类展示
一,前台 , views/main/index.html ,通过get传送给后台 思路 : 将栏目ID 传递给后台,后台根据 栏目的ID 返回相应的数据 {% if category == '' ...