写在前面的话:

  看了看自己的博客,从一月底开始就没怎么更新过,我也确实将近5个月没怎么写代码了。今天突然觉得有些心慌,感觉手都已经生疏了。果然,随便找了道题就卡住了。隐约感觉要用map但又不太记得用法了,知道可以用DFS或BFS却又理不清思路。费了两个小时,结果写了一个shit一样的代码才通过。唉好忧伤啊。

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/ \\_/

我的解法:

反应了好久,才发现题目的难点是防止结点的重复建立。我的方法没有用map,很繁琐。

#include<iostream>
#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std;struct UndirectedGraphNode {int label;vector<UndirectedGraphNode *> neighbors;UndirectedGraphNode(int x) : label(x) {};};
class Solution {
public:UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {//获取所有独立的结点vector<UndirectedGraphNode *> UniqueNodes;getUniqueNodes(node, UniqueNodes);return findNode(node, UniqueNodes);}//查找指定结点并返回UndirectedGraphNode * findNode(UndirectedGraphNode * node, vector<UndirectedGraphNode *> UniqueNodes){if(NULL == node)return NULL;for(int i = 0; i < UniqueNodes.size(); i++){if(node->label == UniqueNodes[i]->label){return UniqueNodes[i];}}return NULL;}//获取图中所有的结点和连接信息void getUniqueNodes(UndirectedGraphNode *node, vector<UndirectedGraphNode *>& UniqueNodes){//结点空或已存在时直接返回if(NULL == node || NULL != findNode(node, UniqueNodes))return;//存储新出现的结点UndirectedGraphNode * newNode = new UndirectedGraphNode(node->label);UniqueNodes.push_back(newNode);for(int i = 0; i < node->neighbors.size(); i++){getUniqueNodes(node->neighbors[i], UniqueNodes);newNode->neighbors.push_back(findNode(node->neighbors[i], UniqueNodes));}}
};int main()
{Solution s;UndirectedGraphNode * node0 = new UndirectedGraphNode(0);UndirectedGraphNode * node1 = new UndirectedGraphNode(1);UndirectedGraphNode * node2 = new UndirectedGraphNode(2);node0->neighbors.push_back(node1);node0->neighbors.push_back(node2);node1->neighbors.push_back(node2);node2->neighbors.push_back(node2);UndirectedGraphNode * newNode = s.cloneGraph(node0);return 0;
}

网上大神解法

/*** Definition for undirected graph.* struct UndirectedGraphNode {*     int label;*     vector<UndirectedGraphNode *> neighbors;*     UndirectedGraphNode(int x) : label(x) {};* };*/
class Solution {
private:unordered_map<UndirectedGraphNode*,UndirectedGraphNode*> hash;
public://BFSUndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {if(!node) return NULL;queue<UndirectedGraphNode*> Qu;Qu.push(node);hash[node] = new UndirectedGraphNode(node->label);while(!Qu.empty()){UndirectedGraphNode * tmp = Qu.front();Qu.pop();for(UndirectedGraphNode * neighbor : tmp->neighbors){if(hash.find(neighbor) == hash.end()){hash[neighbor] = new UndirectedGraphNode(neighbor->label);Qu.push(neighbor);}hash[tmp]->neighbors.push_back(hash[neighbor]);}}return hash[node];}//DFSUndirectedGraphNode *cloneGraph1(UndirectedGraphNode *node) {if(!node) return NULL;if(hash.find(node) == hash.end()){hash[node] = new UndirectedGraphNode(node->label);for(UndirectedGraphNode* neighbor : node->neighbors){hash[node]->neighbors.push_back(cloneGraph1(neighbor));}}return hash[node];}
};

【leetcode】clone-graph相关推荐

  1. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 1 /* 2 // Definition for a Node. 3 ...

  2. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  3. 【重点!DFS/记忆化递归 + BFS】LeetCode 133. Clone Graph

    LeetCode 133. Clone Graph Solution1: DFS/记忆化递归,参考网址:http://www.cnblogs.com/grandyang/p/4267628.html ...

  4. 【LeetCode】2022 7月 每日一题

    [LeetCode]2022 7月 每日一题 前言 七月太忙了,又是项目又是练车又是各种比赛.大概有10天的每日一题没有当天写完(虽然后面补上了). 将每日一题的所有思路记录在这里分享一下. 7.1 ...

  5. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  6. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  7. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  8. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  9. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  10. 【Leetcode】103. 二叉树的锯齿形层次遍历

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

最新文章

  1. 数据结构----单链表增删改查
  2. 为何Google将几十亿行源代码放在一个仓库?| CSDN博文精选
  3. 自学python什么时候能够兼职-总结下我是如何利用python在闲余时间月赚5千外快...
  4. Py之paddlehub:paddlehub的简介、安装、使用方法之详细攻略
  5. PyCharm运行出现 Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run
  6. ADO.NET三个基本对象(一)
  7. OpenMP、MPICH与OpenMPI
  8. python 编译成exe vmp加密_[分享]某vmp壳原理分析笔记
  9. axture动画原型制作_AxureUX手机移动端交互原型通用模板精简版
  10. java ppt转图片,怎么用POI将PPT的内容转换为图片
  11. 初探数据湖(Data Lake),到底有什么用?让我们来一窥究竟...
  12. oracle漏洞修补,01-oracle漏洞修复
  13. 【06月21日】北上资金持股比例排名
  14. java-web6-Servlet知识
  15. html如何转换为母版页,如何在模板中指定/或取消母版页
  16. 基于SSM的学生考勤管理系统的设计与实现
  17. Unity-Creating Project folder failed!
  18. 痴呆患者血脑屏障(Blood-Brain Barrier, BBB)功能测量
  19. opencv入门:使用交互式进行前景提取
  20. HackTheBox-baby WAFfles order

热门文章

  1. 请允许我悄悄的爱你一次好吗 zz
  2. 树与二叉树(c/c++)
  3. input type=range标签用法实例代码
  4. 【笔记】shellcode相关整理
  5. RouterOS DNS劫持 -- A记录
  6. magento 给My Account,Log In,Contact Us链接增加nofollow标签
  7. 写给想用技术改变世界的年轻人-by 沃兹
  8. eclipse+ADT 进行android应用签名打包详解
  9. CSS的未来:游戏的变革Flexbox
  10. MySQL配置文件my.cnf中文版(转载)