【leetcode】133. Clone Graph
题目如下:
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a
label
(int
) and a list (List[UndirectedGraphNode]
) of itsneighbors
. There is an edge between the given node and each of the nodes in its neighbors.OJ's undirected graph serialization (so you can understand error output):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
/ \
\_/Note: The information about the tree serialization is only meant so that you can understand error output if you get a wrong answer. You don't need to understand the serialization to solve the problem.
解题思路:深拷贝node。题目本身不难,由于所有节点的lable都是唯一的,因此需要保持已经创建过节点的lable,避免出现重复创建。另外节点存在self-cycle,所以遍历过的路径也需要保存。
代码如下:
# Definition for a undirected graph node
class UndirectedGraphNode:
def __init__(self, x):
self.label = x
self.neighbors = [] class Solution:
# @param node, a undirected graph node
# @return a undirected graph node
def cloneGraph(self, node):
if node == None:
return None
root = UndirectedGraphNode(node.label)
queue = [(node,root)]
dic = {}
dic[root.label] = root
dic_visit = {}
while len(queue) > 0:
n,r = queue.pop(0)
if n.label in dic_visit:
continue
for i in n.neighbors:
if i.label not in dic:
i_node = UndirectedGraphNode(i.label)
dic[i.label] = i_node
else:
i_node = dic[i.label]
r.neighbors.append(i_node)
queue.append((i, r.neighbors[-1]))
dic_visit[n.label] = 1 return root
【leetcode】133. Clone Graph的更多相关文章
- 【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 ...
- 【LeetCode】133. Clone Graph 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【Lintcode】137.Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- 【LeetCode】133. 克隆图
133. 克隆图 知识点:图:递归;BFS 题目描述 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[No ...
- 【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 ...
随机推荐
- 【串线篇】MyBatis简介
一.MyBatis 和数据库进行交互:持久化层框架(SQL映射框架): 1).纯手工 从原始的JDBC----dbutils(QueryRunner)-------JdbcTemplate----xx ...
- githup上传项目到仓库
1.有了自己的账号 2.创建一个新的项目,填写项目名称,描述 填写完成点击create repository 3.复制生成的https链接接下来用到 4.进入到你的项目所在目录右键git bash打开 ...
- CSIC_716_20191224【python基础结课作业--仿优酷练习】
需 求:********管理员界面******** 1 注册 2 登录 3 上传视频 4 删除视频 5 发布公告 ********普通用户界面******** 1 注册 2 登录 3 冲会员 4 查看 ...
- php time()函数 语法
php time()函数 语法 time()函数怎么用? php time()函数用来返回当前时间的unix时间戳.语法是time(),返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 ...
- 快速搜索插件之quicksearch
项目中如果遇到快速搜索table表格,select下拉框,ul>li列表等内容的时候,可以使用quicksearch快速搜索插件,举个栗子: 首先要引用jquery.js + jquery.qu ...
- nginx防止SQL注入规则
$request_uriThis variable is equal to the *original* request URI as received from the client includi ...
- 用JOptionPane类实现各种对话框
用JOptionPane类实现各种对话框 运行结果: 下面部分参考: JOptionPane类提示框的一些常用的方法 - - ITeye博客 http://847353020-qq-com.itey ...
- HTML5: HTML5 表单属性
ylbtech-HTML5: HTML5 表单属性 1.返回顶部 1. HTML5 表单属性 HTML5 新的表单属性 HTML5 的 <form> 和 <input>标签添加 ...
- https://www.cnblogs.com/limanjihe/p/10184327.html
https://www.cnblogs.com/limanjihe/p/10184327.html https://blog.csdn.net/xnnswmzdszyd/article/details ...
- 32. 持续集成简介及JDK、Tomcat、Jenkins环境搭建
持续集成简介 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每天至少集成一次,也就意味着每天可能会发生多次集成.每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验 ...