- 浏览: 183517 次
- 性别:
- 来自: 济南
文章分类
最新评论
之所以把这道题单独拿出来,是因为通过它我们可以了解到图的结构,以及如何处理,我们分别用递归,广搜和深搜来完成这道题。
Clone Graph
复制一个无向图,图中每个节点都有一个label和一个neighbors集合。
解决图的题,因为图中存在环,我们要判断哪些点已经访问过了,做上标记,以防止进入死循环。这里我们用哈希函数来判断一个顶点是否被访问过,如果没被访问过就加入到哈希表中。
首先我们通过广搜来完成它。广搜用到队列,代码如下:
深度搜索用到堆栈,代码如下:
递归实现:
Clone Graph
复制一个无向图,图中每个节点都有一个label和一个neighbors集合。
解决图的题,因为图中存在环,我们要判断哪些点已经访问过了,做上标记,以防止进入死循环。这里我们用哈希函数来判断一个顶点是否被访问过,如果没被访问过就加入到哈希表中。
首先我们通过广搜来完成它。广搜用到队列,代码如下:
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); if(node == null) return null; UndirectedGraphNode copy = new UndirectedGraphNode(node.label); hm.put(node,copy); queue.offer(node); while(!queue.isEmpty()) { UndirectedGraphNode cur = queue.poll(); for(UndirectedGraphNode neighbor : cur.neighbors) { if(!hm.containsKey(neighbor)) { copy = new UndirectedGraphNode(neighbor.label); hm.put(neighbor, copy); queue.offer(neighbor); } hm.get(cur).neighbors.add(hm.get(neighbor)); } } return hm.get(node); } }
深度搜索用到堆栈,代码如下:
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); Stack<UndirectedGraphNode> stack = new Stack<UndirectedGraphNode>(); if(node == null) return null; stack.push(node); UndirectedGraphNode copy = new UndirectedGraphNode(node.label); hm.put(node,copy); while(!stack.isEmpty()) { UndirectedGraphNode cur = stack.pop(); for(UndirectedGraphNode neighbor : cur.neighbors) { if(!hm.containsKey(neighbor)) { copy = new UndirectedGraphNode(neighbor.label); hm.put(neighbor, copy); stack.push(neighbor); } hm.get(cur).neighbors.add(hm.get(neighbor)); } } return hm.get(node); } }
递归实现:
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); return DFS(hm, node); } private UndirectedGraphNode DFS(HashMap<UndirectedGraphNode, UndirectedGraphNode> hm, UndirectedGraphNode node) { if(node == null) return null; UndirectedGraphNode copy = new UndirectedGraphNode(node.label); hm.put(node, copy); for(UndirectedGraphNode neighbor : node.neighbors) { if(!hm.containsKey(neighbor)) { UndirectedGraphNode neighborcopy = DFS(hm, neighbor); } copy.neighbors.add(hm.get(neighbor)); } return copy; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
leetcode CloneGraph java 源代码
https://www.lintcode.com/problem/clone-graph/description Deep copy一个图。图以邻接表方式存储。 思路是,先从给定的顶点出发,搜索到图中的所有的顶点,然后为每个顶点创建一份拷贝;接着,遍历原图的顶点,每...
133.Clone_Graph_克隆图【LeetCode单题讲解系列】
python python_leetcode题解之133_Clone_Graph
javascript js_leetcode题解之133-clone-graph.js
`cloneGraph`函数的目的是接收一个`Node`对象并返回一个新的、完全独立的`Node`对象,所有节点及其连接关系都要被正确地复制。 解决方案是使用深度优先搜索策略。首先,创建一个空的`map_list`字典,用于存储原图中...
杂项部分包括了一些不那么容易归类的问题,如螺旋矩阵(Spiral Matrix)、整数转罗马数字(Integer to Roman)、克隆图(Clone Graph)等。 **栈(Stack)** 栈是一种先进后出(FILO)的数据结构。 - 最小栈(Min ...
38. Clone Graph:复制一个无向图。 39. Min Stack:设计一个栈,支持获取栈内最小元素的操作。 40. Evaluate Reverse Polish Notation:计算后缀表达式的值。 41. Valid Parentheses:判断字符串是否为有效的括号...
38. Clone Graph:深度复制一个图。 【栈】 39. Min Stack:设计一个栈,支持 push、pop、top 操作,并且在常数时间内得到栈的最小值。 40. Evaluate Reverse Polish Notation:计算后缀表达式。 41. Valid ...
- **CloneGraph(克隆图)**:复制一个图,要求每个节点只复制一次。 - **NQueens(N皇后问题)**:在一个N×N的棋盘上放置N个皇后,要求不相互攻击。 - **SixDegrees(六度空间理论)**:找出两个人之间的联系路径...
**1.19 Clone Graph (133)** - **问题描述**:给定一个无向图,对其进行深拷贝。 - **解题思路**: - 使用深度优先搜索(DFS)或广度优先搜索(BFS)遍历整个图。 - 对于每个访问过的节点,创建一个副本,并将...
与大家分享我学习算法的一些经历。这个项目不定期更新。数组/链表:树相关:AVLTree 平衡二叉搜索树...BFSGraph 图BFS模板Dijkstra 寻求最短路SwimmingCrossSea 漂洋过海CloneGraph (leetcode 133)字符串相关:Reve
Clone Graph, medium --- 类似于#138 207,课程表,中等 210 课程表 II,中等 261,图有效树,中等 310,最小高度树,中等 323,无向图中连通分量的数量,中等 444,序列重建,中 第二周——堆 215,数组中的第 K 个...
We propose a new variant of LSH scheme and incorporate it with graph matching to address these challenges. We implement an integrated assembly clone search engine called Kam1n0. It is the first clone...
If you clone the whole project, main app is a sample app that you can run and see simplegraph functionality in action. Why this library is out there? Well, I've spent too much time looking for library...
例如,"Clone Graph"题目中,通过DFS克隆一个无向图;"Shortest Path in Binary Matrix"则需要BFS寻找矩阵中最短路径。 总结,通过LeetCode的实践,我们可以提升JavaScript编程技巧,熟练掌握各种数据结构和算法,...
LDBC SNB在Janusgraph上... 解压缩 在配置了后端存储的情况下运行Janusgraph,请参阅bin/janusgraph start|status|stop|clean# bin/gremlin-server.sh #need configure backend storage克隆并构建git clone git@github....
$ sudo apt install libpcre2-dev libxml2-dev签出存储库并初始化所有子模块 $ git clone https://github.com/kuopinghsu/callgraph-gen.git$ cd callgraph-gen$ git submodule update --init --recursive建造 $ ...
依存关系本征3.3或更高版本Ceres Solver 1.12.0或更高版本Gflags 2.2.0或更高版本带有matplotlib的Python建造$ git clone https://github.com/shinsumicco/pose-graph-optimization.git$ cd pose-graph-optimization...