题目:

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. IsPostBack用法

    可以自己定义 在页面中定义隐藏的input,设置为ispostback. <form action="" method=""> <input ...

  2. 51. N-Queens (Array; Back-Track, Bit)

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  3. ReportViewer工具栏功能扩展[手动设置打印/导出按钮]

    ReportViewer在IE11后打印按钮就存在兼容问题,火狐,谷歌也存在打印按钮显示的兼容性问题,本资料就是解决ReportViewer打印按钮显示的问题, 通过自己写脚本添加到DOM里面让所有浏 ...

  4. 'WebElement' object is not iterable

    checkbox.html源码: <html> <head> <meta http-equiv="content-type" content=&quo ...

  5. nginx反向代理架构与安装配置(一)

    这里我们准备四台虚拟机,二台负载均衡(LB01,LB02),二台web服务器(WEB01,WEB02).   这里默认所有软件都安装在/data目录下.   四台虚拟机的初始安装是centos7的最小 ...

  6. win下php5.4安装ffmpeg-php扩展

    1.ffmpeg的官网没有提供ffmpeg-php dll的扩展下载. http://ffmpeg-php.sourceforge.net/ 虽然在http://sourceforge.net/上提供 ...

  7. 给dede添加栏目图片和栏目描述

    有的时候我们希望调用栏目时把栏目的图片和描述调出来,但dede好像没有提供栏目图片这个功能,而栏目的描述也是给meta:Description使用的,不是很方便.   所以我们需要自已给dede添加图 ...

  8. 767A Snacktower

    A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. mysq 日期l查询

    pym=mysql(host = '#', port = 3306, user = '#',passworld='#',database='#') #根据起始和结束时间 charge_sql = 'S ...

  10. 想要打动HR的心,UX设计师求职信究竟应该怎么写?

    在努力准备申请一份UX设计师职位时,你最烦心和担忧的事哪一个环节?是写一份UX设计师简历?回答面试官的问题?还是在一遍遍的煎熬中等待一个面试电话?是的,这些都是不轻松的事儿,但还有一个同样糟心的事,那 ...