题目:

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. webpack 构建同时适用于手机和电脑的调试服务器

    plugins plugins: [ new HtmlWebpackPlugin({ // 使用模板同时生成 pc.html和mobile.html title: 'pc', filename: 'p ...

  2. 使用maven将项目热发布到tomcat7的坑

    首先是配置tomcat的用户权限问题,最好是配置最大的权限,要不然会报错,我之前就是一直报错 <role rolename="manager"/> <user u ...

  3. c++泛型模板

    模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成员或者成员函数的参数.返回值取得任意类型. 模板是一种对类型进行参数化的工具: 通常有两种形式:函 ...

  4. 详解html中的元老级元素:“table”

    table标签历史悠久,在互联网出现的早期,web网页的排版主要是靠table表格,对web网页做出了不可磨灭的贡献,直到后来层叠样式表:CSS的发展完善,再配合空元素DIV,才有了今天绚丽多彩的网页 ...

  5. hg 添加用户

    .hg目录下hgrc文件 [ui] username = lyd

  6. VMware下的Linux系统中Windows的共享目录,不支持创建软连接

    [问题]  在编译VMware下的Linux系统对从Windows中共享过来的文件,进行编译的时候,遇到:  ln: creating symbolic link XXXXXX : Operation ...

  7. 用个体软件过程(PSP)记录你的工作

    用个体软件过程(PSP)记录你的工作 首先,非常感谢大家对本门课程的学习所投入的时间和精力. 其次,已经进入数据时代,口说无凭,拿数据来.如果你认为你已经投入了大量精力在这门课程的学习和作业中,而且已 ...

  8. RESTful Web API 实践

    REST 概念来源 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备...). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通 ...

  9. Criteria查询

    1.Criteria表达式 Criteria c=session.createCriteria(User.class); List result=c.list(); Iterator it=resul ...

  10. python编码(六)

    1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码.计算机世界里一开始只有 ...