clone-graph leetcode C++
Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.
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 as0. Connect node0to both nodes1and2. Second node is labeled as1. Connect node1to node2. Third node is labeled as2. Connect node2to node2(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
C++
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node){
unordered_map<int, UndirectedGraphNode*> umap;
return clone(node, umap);
}
UndirectedGraphNode *clone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &umap) {
if (!node) return node;
if (umap.count(node->label)) return umap[node->label];
UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label);
umap[node->label] = newNode;
for (int i = 0; i < node->neighbors.size(); ++i){
(newNode->neighbors).push_back(clone(node->neighbors[i],umap));
}
return newNode;
}
};
clone-graph leetcode C++的更多相关文章
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- Clone Graph [LeetCode]
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- Clone Graph——LeetCode
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- 【LeetCode】133. Clone Graph (3 solutions)
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- LeetCode: Clone Graph 解题报告
Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
随机推荐
- C# .NET Core 3.1 中 AssemblyLoadContext 的基本使用
C# .NET Core 3.1 中 AssemblyLoadContext 的基本使用 前言 之前使用 AppDomain 写过一个动态加载和释放程序的案例,基本实现了自己"兔死狗烹&qu ...
- Selenium系列5-XPath路径表达式
Xpath介绍 XPath 使用路径表达式在 XML 文档中进行导航 XPath 使用路径表达式来选取 XML 文档中的节点或者节点集.这些路径表达式和我们在常规的电脑文件系统中看到的表达式非常相似. ...
- Sentry 监控 - Environments 区分不同部署环境的事件数据
系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...
- .Net Core with 微服务 - 分布式事务 - 可靠消息最终一致性
前面我们讲了分布式事务的2PC.3PC , TCC 的原理.这些事务其实都在尽力的模拟数据库的事务,我们可以简单的认为他们是一个同步行的事务.特别是 2PC,3PC 他们完全利用数据库的事务能力,在一 ...
- requests接口自动化-数据库参数化
数据库参数化的场景 部分接口,参数其他方式获取不到,可以去数据库去获取,如手机验证码 安装pymysql 配置文件 def sql_conf(): # 定义数据库的配置 host='127.0.0.1 ...
- hibernate 初学
1. hibernate的基本操作 执行流程: 执行流程细节:基本的配置文件 可以与mybatis进行对比着记 hibernate 的主键生成策略 ...
- linux mint17.3+vmware 12.1.1 流畅安装运行OSX EI capitan
在linux mint17.3的vmware虚拟机中安装mac osx ei capitan系统 出于对苹果操作系统的好奇与喜爱,分别在宿主机操作系统为windows 7和linux mint17.3 ...
- JuiceFS v0.17 发布,通过 1270 项 LTP 测试!
小伙伴们大家好,JuiceFS v0.17 在国庆小长假来临之际如期发布了!这是我们在 2021 年秋季推出的第二个版本,让我们直奔主题,看看都有哪些新变化吧. 本次更新累计 80+ 提交,共有 9 ...
- 理解hashMap
首先需要理解几个基本概念: 什么是数据结构?(摘自 java数据结构系列--什么是数据结构 (baidu.com)) 数据结构是计算机组织.存储数据的方式.简单来说就是,数据按指定的规则进行存储,从而 ...
- react之组件数据挂在方式
1.属性(props) 组件间传值,在React中是通过只读属性 props 来完成数据传递的. props:接受任意的入参,并返回用于描述页面展示内容的 React 元素. import React ...