【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 node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to 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 ...
随机推荐
- golang thrift 总结一下网络上的一些坑
我们以hello world来大概分析一下golang中的thrift包,并且扒一扒网络上有关thrift的一些坑 查看源码,服务器定义如下:(详见simple_server.go文件) type T ...
- 洛谷U5653 宋荣子的小饼干
题目描述 楼下机房的LYL有n个妹子,分别编号为a1,a2……an,每个妹子都拥有一定数量的小饼干.有一天,saruka没有吃晚饭,饿的不要不要的,这时,他忽然想起了LYL的妹子们有小饼干可以吃.于是 ...
- sdibt 1244 烦人的幻灯片
在这个OJ站还没号,暂时没提交,只是过了样例 真不愧是烦人的幻灯片,烦了我一小时 ---更新:OJ测试完毕,AC 烦人的幻灯片问题 Time Limit: 1 Sec Memory Limit: 6 ...
- html checkbox 全选与反选
之所以记录这个博客,是因为我完全用jquery,无法实现自己想要的结果(通过一个checkbox的选中或不选中控制其他多个checkbox状态) <input type="checkb ...
- php 获取后缀的几种方法
1: function get_extension($file){ substr(strrchr($file, '.'), 1); } 2: function get_extension($file) ...
- iOS视图生命周期与视图控制器关系
iOS中视图是一个应用的重要组成部分,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻. 视图生命周期与视图控制器关系 以视图的5种状态为基础,我们来系统的了解一下 ...
- 标准库errno.h 查看错误代码编号,errno:4 与error:2
log 里报错,errno:4 与errno:2 查了一下 errno.h --------下文来自百度百科 errno 编辑 errno 是记录系统的最后一次错误代码.代码是一个int型的值 ...
- HDU 1018 Big Number (数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x ...
- iOS团队开发者测试
那么你需要在你下载证书的那个电脑上从钥匙串-->选择证书-->右键到处证书,保存为.p12的证书,以后这个证书拷贝到任何电脑上去都是可以使用的! 本来只有一台电脑可以测试, 现在要团队开发 ...
- @version ||= version
# -*- encoding : utf-8 -*- class InterfaceBaseController < ActionController::Base private def set ...