写在前面的话:

  看了看自己的博客,从一月底开始就没怎么更新过,我也确实将近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. Science | 以功能为核心的蛋白质设计
  2. [LintCode] Single Number 单独的数字
  3. 笔记:基于标签的推荐系统、基于图的推荐算法、PersonalRank
  4. php的字符串处理总结,php字符串处理函数总结
  5. nssl1211-好文章【字符串hash,map】
  6. VC++2010配置使用MySQL5.6
  7. eclipse 输入提示插件_【STM32】搭建基于Eclipse平台的STM32调试环境
  8. centos下安装VMware Server
  9. LaTeX技巧:算法标题 Algorithm如何重命名
  10. 【图像几何】基于matlab投影法测距【含Matlab源码 405期】
  11. spring与spring mvc
  12. 压力测试 JMeter 使用教程(三分钟拿下)
  13. 银河麒麟V10 SELinux启动问题
  14. 解决SSLHandshakeException :sun.security.validator.ValidatorException: PKIX path building failed:
  15. 国外问卷调查做快点有影响吗
  16. Mysql——分组查询
  17. 函数重载的条件、作用、注意事项
  18. C++面向对象(一):面向对象程序设计概述
  19. 文库网站建设源码分享
  20. 洛谷 P4315 月下“毛景树”(边树剖)

热门文章

  1. Servlet架构初解析
  2. 阿里云Kubernetes服务 - Service Broker快速入门指南
  3. 【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)...
  4. Android单元測试之JUnit
  5. 为什么你喜欢的女生不喜欢你
  6. openwrt开发过程简介
  7. MS CRM 2011 RC中的新特性(2)——销售自动化方面
  8. [管理心得] 稻盛和夫为日航危机出诊--人情营销的典型
  9. Forefront Client Security服务器配置
  10. C#学习系列之二:变量