【leetcode】clone-graph
写在前面的话:
看了看自己的博客,从一月底开始就没怎么更新过,我也确实将近5个月没怎么写代码了。今天突然觉得有些心慌,感觉手都已经生疏了。果然,随便找了道题就卡住了。隐约感觉要用map但又不太记得用法了,知道可以用DFS或BFS却又理不清思路。费了两个小时,结果写了一个shit一样的代码才通过。唉好忧伤啊。
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
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 node0to both nodes1and2. - Second node is labeled as
1. Connect node1to node2. - Third node is labeled as
2. Connect node2to node2(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 = ; 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 = ; 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();
UndirectedGraphNode * node1 = new UndirectedGraphNode();
UndirectedGraphNode * node2 = new UndirectedGraphNode();
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 ;
}
网上大神解法
/**
* 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:
//BFS
UndirectedGraphNode *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];
}
//DFS
UndirectedGraphNode *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的更多相关文章
- 【leetcode】Clone Graph(python)
类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点, ...
- 【LeetCode】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- opencv笔记6:角点检测
time:2015年10月09日 星期五 23时11分58秒 # opencv笔记6:角点检测 update:从角点检测,学习图像的特征,这是后续图像跟踪.图像匹配的基础. 角点检测是什么鬼?前面一篇 ...
- POJ 1466 Girls and Boys
Girls and Boys Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...
- hihocoder #1285 智力竞赛
传送门 总结: 1.仔细读题 2.仔细分析复杂度 3.不要想当然,乱下结论 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队 ...
- codeforce626D (概率)
D. Jerry's Protest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 初学Hibernate
Hibernate 是完全ORM的,只需要对 对象 进行操作,生成底层SQL语句 优势:1.可以简化开发 2.性能好(原生的Hibernate性能很差,要使用它,需要进行优化),优化方式:一级缓存.二 ...
- 位图索引:原理(BitMap index)
http://www.cnblogs.com/LBSer/p/3322630.html 位图(BitMap)索引 前段时间听同事分享,偶尔讲起Oracle数据库的位图索引,顿时大感兴趣.说来惭愧,在这 ...
- mybatis使用小记
参考资料:http://blog.csdn.net/hupanfeng/article/details/9098453 1.设置不缓存每次查询的结果: 如题,通过设置 flushCache=" ...
- 织梦DedeCms用SQL语句调用数据库任意内容方法
织梦DedeCms给我们提供了大量调用标签,供我们调用各种数据,但提供再多的标签,也有满足不了我们的时候,这时我们可以用SQL语句,灵活调用我们需要的内容. 如何任意调用数据库中的内容呢?先举个例子: ...
- 通用跨站脚本攻击(UXSS)
有同学问,用百度搜索了下,发现国内相关介绍基本是没有,就写篇文章来介绍下.不过看到有现成的介绍,就拿来翻译修改下.本文的内容主要翻译来自该文章,把一些没必要的话给删了,做了一些整理修改,然后补充一些案 ...
- SQL2005之SA提权总结
首先,看看xp_cmdshell存在不,不存在的话先恢复下. Exec sp_configure 'show advanced options',1;RECONFIGURE;EXEC sp_confi ...