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单题讲解系列】
Clone Graph 位操作 Reverse Bits Repeated DNA Sequences Number of 1 Bits Gray Code Single Number Single Number II Single Number III Power of Two Missing Number Maximum Product of Word Lengths Bitwise ...
与大家分享我学习算法的一些经历。这个项目不定期更新。数组/链表:树相关: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...
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建造 $ ...
git clone --recursive https://github.com/andydbc/unity-shadergraph-sandbox.git 现有存储库可以手动更新: git submodule init git submodule update 例子 溶解 火 全息图 像素化 香椿 简单标志 要求 ...
依存关系本征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...
$ git clone https://github.com/KKhanda/graph-db.git 生成并运行应用程序 $ go build ./cmd/graph-db/ $ ./graph-db 运行测试 $ go test ./... -cover API说明 API使用示例附在“ main.go”文件中。
git clone https://github.com/VirusTotal/vt_graph_apicd vt_graph_apipip install . --user验证安装>> > import vt_graph_api>> > vt_graph_api . __version__X . X . X文献资料有关如何使用vt_graph_api的更多...
$ git clone https://github.com/danini/graph-cut-ransac$ cd build$ cmake ..$ make安装Python包并编译C ++ python3 ./setup.py install 或者pip3 install -e .示例项目要构建显示基本矩阵,单应性和基本矩阵
图形**** ****BFS class Solution { public Node cloneGraph ( Node node ) { if (node == null ) { return null ; } Map< Node> map = new HashMap<> (); Queue< Node> queue = new LinkedList<> (); queue ....
git clone https://github.com/kadukeitor/keep-graph.git 2.移至项目 cd keep-graph 3.安装依赖项 npm install 4.创建配置文件 cp .env.example .env 4.1。 当前配置 REACT_APP_GRAPHQL_ENDPOINT_SIMPLE=...
# clone project git clone https://github.com/YourGithubName/your-repo-name cd your-repo-name # optionally create conda environment conda update conda conda env create -f conda_env.yaml -n your_env_...
npm的软件包依赖图 该项目呈现npm软件包的依赖关系图。 它使用获取包元数据,使用绘制图形,使用进行... $ git clone https://github.com/TypeFox/npm-dependency-graph.git $ cd npm-dependency-graph $ yarn 作为
git clone https://github.com/rocurley/Nations-Graph.git cd Nations-Graph cabal sandbox init cabal install --only-dependencies 要下载图形: #the download command requres a maximum number of pages to ...