- 浏览: 183411 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 node 0 to both nodes 1 and 2.
Second node is labeled as 1. Connect node 1 to node 2.
Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 -- 2
/ \
\_/
题目的要求是复制一个图。图中的节点有两个属性,一个是label,一个是一个邻居链表,包含着与当前节点相连的节点,换句话说就是当前节点与邻居链表中的每个点之间都有一条边。这里我们借助一个哈希表来记录当前节点和它的复制节点的对应关系。实现代码如下:
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 node 0 to both nodes 1 and 2.
Second node is labeled as 1. Connect node 1 to node 2.
Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 -- 2
/ \
\_/
题目的要求是复制一个图。图中的节点有两个属性,一个是label,一个是一个邻居链表,包含着与当前节点相连的节点,换句话说就是当前节点与邻居链表中的每个点之间都有一条边。这里我们借助一个哈希表来记录当前节点和它的复制节点的对应关系。实现代码如下:
/** * 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) { HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); if(node == null) return node; UndirectedGraphNode copy = new UndirectedGraphNode(node.label); hm.put(node, copy); queue.offer(node); while(!queue.isEmpty()) { UndirectedGraphNode cur = queue.poll(); for(UndirectedGraphNode neighbor : cur.neighbors) { if(!hm.containsKey(neighbor)) { copy = new UndirectedGraphNode(neighbor.label); hm.put(neighbor, copy); queue.offer(neighbor); } hm.get(cur).neighbors.add(hm.get(neighbor)); } } return hm.get(node); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
133.Clone_Graph_克隆图【LeetCode单题讲解系列】
`cloneGraph`函数的目的是接收一个`Node`对象并返回一个新的、完全独立的`Node`对象,所有节点及其连接关系都要被正确地复制。 解决方案是使用深度优先搜索策略。首先,创建一个空的`map_list`字典,用于存储原图中...
Keep Graph是开源克隆。 它建立在和。 当前后端托管在 入门 1.克隆项目 git clone https://github.com/kadukeitor/keep-graph.git 2.移至项目 cd keep-graph 3.安装依赖项 npm install 4.创建配置文件 cp .env....
软件了拢检测技术研究,让你快速了解软件克隆的来龙去脉
git clone --recursive https://github.com/andydbc/unity-shadergraph-sandbox.git 现有存储库可以手动更新: git submodule init git submodule update 例子 溶解 火 全息图 像素化 香椿 简单标志 要求 ...
LDBC SNB在Janusgraph上... 解压缩 在配置了后端存储的情况下运行Janusgraph,请参阅bin/janusgraph start|status|stop|clean# bin/gremlin-server.sh #need configure backend storage克隆并构建git clone git@github....
图切RANSAC 论文中提出的图切RANSAC算法:Daniel Barath和Jiri Matas; Graph-Cut RANSAC,计算机视觉和模式识别会议,2018年。可从以下获得: CVPR教程解释了该方法。 有关单应性,基本矩阵,基本矩阵和6D姿态估计...
graph-alg-viz-使用D3的图形算法的可视化。 这个项目是一个应用程序,它使用 (或更确切地说,很快就会出现... git clone https://github.com/lockoff/graph-alg-viz.git cd graph-alg-viz 安装依赖项 在这个项目中,
Graph-based code semantics learning for efficient semantic code clone detection CCEyes: An Effective Tool for Code Clone Detection on Large-Scale Open Source Repositories 附有对应的项目源代码链接: ...
克隆此仓库git clone https://github.com/MichelDequick/custom-contribution-graph-generator.git 安装需求pip install -r custom-contribution-graph/requirements.txt 运行程序python custom-contribution-...
克隆git clone git@github.com:nicoespeon/gitgraph.js.git : git clone git@github.com:nicoespeon/gitgraph.js.git 。 使用 npm install --save gitgraph.js : npm install --save gitgraph.js 。 使用 bower ...
rdb2graph-关系数据库...克隆rdb2graph项目 git clone https://github.com/s1ck/rdb2graph 为了正常运行,需要对ddl utils进行修补。 从他们的svn结帐ddlutils svn co http://svn.apache.org/repos/asf/db/ddlutils
如果您不知道如何克隆此存储库,则可以使用上方的绿色“ Clone or download按钮将项目下载为.zip存档。 如果您想与我们讨论有关Shader Graph的信息,请访问论坛并继续关注博客,以获取更多激动人心的更新! 注意:...
以太坊图调试器 图形化EVM调试器。 该调试器采用了与传统调试不同的方法。 它没有逐步执行程序,而是以红色突出显示了整个程序控制流程图和事务的... git clone https://github.com/fergarrui/ethereum-graph-debugger
具有快速局部光谱滤波的图卷积神经网络 该存储库中的代码将流行的卷积神经网络(CNN)有效地概括为任意图,如本文所述: MichaëlDefferrard,Xavier Bresson,Pierre Vandergheynst,,神经信息处理系统(NIPS),...
git clone git@github.com:elsoul/souls_graph.git cd souls_graph bundle install 用法 检查Rakefile以查看可用的命令 rake - T 在本地运行服务器 bundle exec puma - p 3000 - e development 您可以在这里看到...
最初克隆此存储库时,请使用git clone --recursive同时更新子模块。 稍后,运行git submodule update来手动更新子模块。 如果克隆时不使用--recursive开关,请先运行git submodule init初始化子模块。
克隆存储库: git clone https://github.com/Vanya9422/react-apollo-graph.git 转到服务器文件夹并安装依赖项: npm install 运行项目: npm run dev 快速入门申请 转到应用程序文件夹并安装依赖项: yarn ...
圆形知识图 代表技术的可视化 入门 这些说明将为您提供在本地计算机上运行并运行的项目的副本,以进行开发和测试。 先决条件 你需要安装 node 和 npm For OSX brew install node 安装 一个循序渐进的示例系列,...
$ git clone https://github.com/gssequence/population-graph.git$ cd population-graph$ npm i 请注册以使用并获取API密钥。.env.development.local项目的根目录中创建一个.env.development.local文件.env....