题目:

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 #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. 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
/ \
\_/

解题思路:

本题要解决的问题就是对一个图进行clone,这不禁让我们想起图的遍历:DFS和BFS,所以我们可以再遍历图中某个点时,增加一些附加的操作而不仅仅是访问它,这里我们要增加的附加操作即对该点进行clone。
现在我们利用DFS和BFS两种方式进行解题。

实现代码:

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std; /*
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
/ \
\_/ */ struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
}; class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(node == NULL)
return NULL;
unordered_map<int, UndirectedGraphNode*> hashtable;//标志位,是否已拷贝了key为label,value为新拷贝的node
return dfsclone(node, hashtable); } //DFS
UndirectedGraphNode *dfsclone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &hashtable)
{
if(node == NULL)
return NULL;
if(hashtable.count(node->label) > )
return hashtable[node->label];//已经拷贝好了,则直接返回即可,否则进行下面的拷贝操作
UndirectedGraphNode *copyNode = new UndirectedGraphNode(node->label);
hashtable[copyNode->label] = copyNode;
vector<UndirectedGraphNode *>::iterator iter;
for(iter = node->neighbors.begin(); iter != node->neighbors.end(); ++iter)
{
copyNode->neighbors.push_back(dfsclone(*iter, hashtable));//进行深度优先算法拷贝
} return copyNode;
} //BFS
UndirectedGraphNode *bfsclone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &hashtable)
{
if(node == NULL)
return NULL;
queue<UndirectedGraphNode *> qu;
UndirectedGraphNode *copyNode = new UndirectedGraphNode(node->label);
hashtable[copyNode->label] = copyNode;
qu.push(node);
while(!qu.empty())
{
UndirectedGraphNode *tnode = qu.front();
qu.pop();
vector<UndirectedGraphNode *>::iterator iter;
for(iter = node->neighbors.begin(); iter != node->neighbors.end(); ++iter)
{
if(hashtable.count((*iter)->label) == )
{
UndirectedGraphNode *tnode = new UndirectedGraphNode((*iter)->label);
hashtable[(*iter)->label] = tnode;
qu.push(*iter);
}
(hashtable[tnode->label])->neighbors.push_back(hashtable[(*iter)->label]); } }
return copyNode;
}
};
int main(void)
{
return ;
}

LeetCode133:Clone Graph的更多相关文章

  1. 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. ...

  2. 21. Clone Graph

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  3. 133. Clone Graph

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  4. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  5. 【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 ...

  6. LeetCode: Clone Graph 解题报告

    Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...

  7. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  8. 【Clone Graph】cpp

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  9. 【Lintcode】137.Clone Graph

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

随机推荐

  1. nyoj1076-方案数量 【排列组合 dp】

    http://acm.nyist.net/JudgeOnline/problem.php?pid=1076 方案数量 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 ...

  2. 使用Maven部署构件至私服

    --------------------siwuxie095                                 使用 Maven 部署构件至私服         1.部署构件到 Nexu ...

  3. Asp.net 后台调用js方法

    购物车实现逻辑简单.代码量也很少,具体细节就不说了,使用的时候,只要把MockDB类稍微改改,因为它是商品数据入口,为实现分布式部署,实际应用时可以更改为从服务调用,如:Web Service.WCF ...

  4. CSS文字大小单位px、em、pt详解

    这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章,题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用p ...

  5. 【校招面试 之 剑指offer】第9-1题 用两个栈实现一个队列

    #include<iostream> #include<stack> using namespace std; template <typename T> void ...

  6. nginx常用配置说明

    nginx的主配置(nginx.conf)说明 #worker进程数量 worker_processes 1; #错误日志 error_log logs/error.log; #进程ID文件 pid ...

  7. Python 安装路径, dist-packages 和 site-packages 区别

    Stack Overflow's answer 译: dist-packages is a Debian-specific convention that is also present in its ...

  8. Castle ActiveRecord学习(五)使用HQL语句查询

    来源:http://www.cnblogs.com/Terrylee/archive/2006/04/12/372823.html 一.HQL简单介绍HQL全名是Hibernate Query Lan ...

  9. meterpreter 如何留后门,使攻击持久化

    安装后门方法一:meterpreter >run persistence -X -i 5 -p 443 -r 192.168.0.108 Persistent agent script is 6 ...

  10. .net core webapi 部署windows server 2008 r2 笔记

    WebAPI部署文档 安装dotnet-dev-win-x64.1.0.4 安装DotNetCore.1.1.0-WindowsHosting 安装vc_redist.x64 安装Windows6.1 ...