Java for LeetCode 133 Clone Graph
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 #.
First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
Second node is labeled as 1. Connect node 1 to node 2.
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
/ \
\_/
解题思路:
BFS或DFS均可做出,BFS的JAVA实现如下:
- public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
- if (node == null)
- return null;
- Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
- HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
- UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
- queue.add(node);
- map.put(node, newHead);
- while (!queue.isEmpty()) {
- UndirectedGraphNode curr = queue.poll();
- List<UndirectedGraphNode> currNeighbors = curr.neighbors;
- for (UndirectedGraphNode aNeighbor : currNeighbors)
- if (!map.containsKey(aNeighbor)) {
- UndirectedGraphNode copy = new UndirectedGraphNode(
- aNeighbor.label);
- map.put(aNeighbor, copy);
- map.get(curr).neighbors.add(copy);
- queue.add(aNeighbor);
- } else
- map.get(curr).neighbors.add(map.get(aNeighbor));
- }
- return newHead;
- }
DFS 实现如下:
- Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
- public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
- if (node == null)
- return null;
- if (map.containsKey(node))
- return map.get(node);
- UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
- map.put(node, newHead);
- for (UndirectedGraphNode aNeighbor : node.neighbors)
- newHead.neighbors.add(cloneGraph(aNeighbor));
- return newHead;
- }
Java for LeetCode 133 Clone Graph的更多相关文章
- [LeetCode] 133. Clone Graph 克隆无向图
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- leetcode 133. Clone Graph ----- java
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [leetcode]133. Clone Graph 克隆图
题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node neighbor 1 2, 3 2 1 3 1 ...
- Leetcode#133 Clone Graph
原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- 【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 ...
- 133. Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- 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 ...
随机推荐
- Linux shell 提取文件名和目录名的一些方法(转)
很多时候在使用Linux的shell时,我们都需要对文件名或目录名进行处理,通常的操作是由路径中提取出文件名,从路径中提取出目录名,提取文件后缀名等等.例如,从路径/dir1/dir2/file.tx ...
- k8s学习(一)——kubectl与api-server之间的交互核心过程
k8s的架构是用户使用kubectl工具对虚拟机资源进行各种各样的控制和定制. 而kubectl本身并不包含对其核心资源的访问与控制.而是通过http通信与api-server进行交互实现资源的管理. ...
- Linux系统救援模式应用:单用户模式找回密码
利用Linux系统救援模式找回密码 方法一: 开机时手要快按任意键,因为默认时间5s grub菜单,只有一个内核,没什么好上下选的,按e键.升级了系统或安装了Xen虚拟化后,就会有多个显示. 接下来显 ...
- JAVA Eclipse创建Android程序如何实现MainActivity和Fragment相互传递数据
最简单的方法是直接强制生成一个MainActivity的实例,然后可以执行其中的方法,当然也是可以传递参数的 更加复杂的用法可以参考: http://blog.csdn.net/huangyabin0 ...
- Solidworks如何使用Toolbox
Toolbox不仅仅是智能扣件.事实上,一般常见的轴承,螺栓,齿轮都有了,点击右侧的设计库即可展开Toolbox 配置完成后我只留下一个GB 比如我要选一个圆锥滚子轴承,从右边拖进来即可 ...
- C 作用域规则
C 作用域规则 任何一种编程中,作用域是程序中定义的变量所存在的区域,超过该区域变量就不能被访问.C 语言中有三个地方可以声明变量: 在函数或块内部的局部变量 在所有函数外部的全局变量 在形式参数的函 ...
- 浅谈PHP与手机APP开发即API接口开发
API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞生很多专门API服务的公司,如:聚合数据(http ...
- 文件I/O操作为什么叫输入/出流
参考以下文档: http://blog.csdn.net/hguisu/article/details/7418161 我们关注的焦点是错误的,重点不在文件,我们关注的核心是数据流. 这种流可以是文本 ...
- iOS_GET_网络请求
同步的 get 请求 #pragma mark - 同步的 get 请求 - (IBAction)GETSynButtonDidClicked:(UIButton *)sender { // 1.网址 ...
- idea设置自定义图片
看图操作哈: 1. 2. 逼格满满: