Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use#
as a separator for each node, and ,
as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}
.
The graph has a total of three nodes, and therefore contains three parts as separated by #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1 / \ / \ 0 --- 2 / \ \_/
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) { return null; } Queue<UndirectedGraphNode> queue = new LinkedList<>(); HashMap<UndirectedGraphNode,UndirectedGraphNode> hashMap = new HashMap<>(); UndirectedGraphNode head = new UndirectedGraphNode(node.label); hashMap.put(node, head); queue.add(node); while (!queue.isEmpty()) { UndirectedGraphNode poll = queue.poll(); for (UndirectedGraphNode uh : poll.neighbors) { if (!hashMap.containsKey(uh)) { queue.add(uh); UndirectedGraphNode tmp = new UndirectedGraphNode(uh.label); hashMap.put(uh, tmp); } hashMap.get(poll).neighbors.add(hashMap.get(uh)); } } return head; } }
相关推荐
leetcode CloneGraph java 源代码
https://www.lintcode.com/problem/clone-graph/description Deep copy一个图。图以邻接表方式存储。 思路是,先从给定的顶点出发,搜索到图中的所有的顶点,然后为每个顶点创建一份拷贝;接着,遍历原图的顶点,每...
133.Clone_Graph_克隆图【LeetCode单题讲解系列】
python python_leetcode题解之133_Clone_Graph
javascript js_leetcode题解之133-clone-graph.js
`cloneGraph`函数的目的是接收一个`Node`对象并返回一个新的、完全独立的`Node`对象,所有节点及其连接关系都要被正确地复制。 解决方案是使用深度优先搜索策略。首先,创建一个空的`map_list`字典,用于存储原图中...
杂项部分包括了一些不那么容易归类的问题,如螺旋矩阵(Spiral Matrix)、整数转罗马数字(Integer to Roman)、克隆图(Clone Graph)等。 **栈(Stack)** 栈是一种先进后出(FILO)的数据结构。 - 最小栈(Min ...
38. Clone Graph:复制一个无向图。 39. Min Stack:设计一个栈,支持获取栈内最小元素的操作。 40. Evaluate Reverse Polish Notation:计算后缀表达式的值。 41. Valid Parentheses:判断字符串是否为有效的括号...
38. Clone Graph:深度复制一个图。 【栈】 39. Min Stack:设计一个栈,支持 push、pop、top 操作,并且在常数时间内得到栈的最小值。 40. Evaluate Reverse Polish Notation:计算后缀表达式。 41. Valid ...
- **CloneGraph(克隆图)**:复制一个图,要求每个节点只复制一次。 - **NQueens(N皇后问题)**:在一个N×N的棋盘上放置N个皇后,要求不相互攻击。 - **SixDegrees(六度空间理论)**:找出两个人之间的联系路径...
**1.19 Clone Graph (133)** - **问题描述**:给定一个无向图,对其进行深拷贝。 - **解题思路**: - 使用深度优先搜索(DFS)或广度优先搜索(BFS)遍历整个图。 - 对于每个访问过的节点,创建一个副本,并将...
与大家分享我学习算法的一些经历。这个项目不定期更新。数组/链表:树相关:AVLTree 平衡二叉搜索树...BFSGraph 图BFS模板Dijkstra 寻求最短路SwimmingCrossSea 漂洋过海CloneGraph (leetcode 133)字符串相关:Reve
Clone Graph, medium --- 类似于#138 207,课程表,中等 210 课程表 II,中等 261,图有效树,中等 310,最小高度树,中等 323,无向图中连通分量的数量,中等 444,序列重建,中 第二周——堆 215,数组中的第 K 个...
We propose a new variant of LSH scheme and incorporate it with graph matching to address these challenges. We implement an integrated assembly clone search engine called Kam1n0. It is the first clone...
If you clone the whole project, main app is a sample app that you can run and see simplegraph functionality in action. Why this library is out there? Well, I've spent too much time looking for library...
例如,"Clone Graph"题目中,通过DFS克隆一个无向图;"Shortest Path in Binary Matrix"则需要BFS寻找矩阵中最短路径。 总结,通过LeetCode的实践,我们可以提升JavaScript编程技巧,熟练掌握各种数据结构和算法,...
LDBC SNB在Janusgraph上... 解压缩 在配置了后端存储的情况下运行Janusgraph,请参阅bin/janusgraph start|status|stop|clean# bin/gremlin-server.sh #need configure backend storage克隆并构建git clone git@github....
$ sudo apt install libpcre2-dev libxml2-dev签出存储库并初始化所有子模块 $ git clone https://github.com/kuopinghsu/callgraph-gen.git$ cd callgraph-gen$ git submodule update --init --recursive建造 $ ...
依存关系本征3.3或更高版本Ceres Solver 1.12.0或更高版本Gflags 2.2.0或更高版本带有matplotlib的Python建造$ git clone https://github.com/shinsumicco/pose-graph-optimization.git$ cd pose-graph-optimization...