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 ...
随机推荐
- Scut游戏服务器引擎5.6.3.5发布
版本:5.6.3.5 (2013-11-25) 1. 优化实体ChangeKey队列,减少写库IO(默认为5分钟写入一次数据库) 2. 优化Protobuf序列化启用自动GZip压缩,减少Redis内 ...
- mysql赋给用户权限grant all privileges on
查看mysql用户表的结构,Field项都是各类权限限制 Host限制登录的IP,User限制登录的用户,Delete_priv限制删除权限,Grant_priv限制权限授予,Super_priv为超 ...
- OWASP Dependency-Check插件介绍及使用
1.Dependency-Check可以检查项目依赖包存在的已知.公开披露的漏洞.目前良好的支持Java和.NET:Ruby.Node.js.Python处于实验阶段:仅支持通过(autoconf a ...
- 怎样mac上安装apk到连接数据线的手机
高大上的mac俺也用了一段时间了.不知道大家有木有同一个烦恼.曾经在win上的时候仅仅要安装了应用宝之类的手机助手.就能够双击APK,直接安装到连接数据线的手机上,非常方便哈,可是mac上不行.近期找 ...
- 身份证号码正则匹配-javascript
function a(a, b) { return a.test(b) } function b(a) { return a = jQuery.trim(a), 0 == a.length } fun ...
- Java使用笔记之stream和sorted使用
//对象类型stream排序List<User> users = new ArrayList<User>(){ { add(new User("a", &q ...
- 模拟IE各种版本的方法
下载360极速浏览器.开启“兼容模式” 默认会是IE7.可以通过控制台(Ctrl + shift + I)调整各个版本
- IDEA------Error:java:无效的目标发行版:1/7
© 版权声明:本文为博主原创文章,转载请注明出处 使用IDEA发布java web项目时,报错.报错信息如下: 解决方案: 方案一:File-->Settings-->Build,Exec ...
- inflate, findViewById与setContentView的差别与联系
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...
- *Android 多线程下载 仿下载助手
今天带来一个多线程下载的 样例.先看一下效果.点击 下载 開始下载,同一时候显示下载进度.完成下载,变成程 安装,点击安装 提示 安装应用. 界面效果 线程池 ThreadPoolExecutor , ...