leetcode — clone-graph
import java.util.*;
/**
* Source : https://oj.leetcode.com/problems/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
* / \
* \_/
*
*/
public class CloneGraph {
/**
* 克隆无向图
*
* 使用BFS或者DFS
*
* HashMap用来记录已经被克隆过的node
* key:被克隆过的node
* value:由key克隆出来的node
*
* @param node
* @return
*/
public UndirectedGraphNode clone (UndirectedGraphNode node) {
if(node == null) {
return null;
}
Queue<UndirectedGraphNode> queue = new ArrayDeque<UndirectedGraphNode>();
Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
map.put(node, new UndirectedGraphNode(node.label));
queue.offer(node);
while (queue.size() > 0) {
UndirectedGraphNode cur = queue.poll();
UndirectedGraphNode cloneNode = map.get(cur);
if (cloneNode.neighbors == null) {
cloneNode.neighbors = new ArrayList<UndirectedGraphNode>();
}
if (cur.neighbors == null) {
continue;
}
for (UndirectedGraphNode neighbor : cur.neighbors) {
if (map.containsKey(neighbor)) {
cloneNode.neighbors.add(map.get(neighbor));
} else {
UndirectedGraphNode temp = new UndirectedGraphNode(neighbor.label);
cloneNode.neighbors.add(temp);
map.put(neighbor, temp);
queue.offer(neighbor);
}
}
}
return map.get(node);
}
/**
*
* 根据字符串创建图的方法不对。。。待完善
* @param graphSrt
* @return
*/
public static UndirectedGraphNode createGraph (String graphSrt) {
if (graphSrt == null || graphSrt.length() == 0) {
return null;
}
Map<Integer, UndirectedGraphNode> map = new HashMap<Integer, UndirectedGraphNode>();
String[] nodeStrs = graphSrt.split("#");
UndirectedGraphNode first = null;
for (int i = 0; i < nodeStrs.length; i++) {
String[] nodeArr = nodeStrs[i].split(",");
UndirectedGraphNode node = null;
for (int j = 0; j < nodeArr.length; j++) {
Integer label = Integer.parseInt(nodeArr[j]);
if (j == 0) {
if (map.containsKey(label)) {
node = map.get(label);
} else {
node = new UndirectedGraphNode(label);
map.put(label, node);
}
if (first == null) {
first = node;
}
} else {
UndirectedGraphNode neighbor = null;
if (map.containsKey(label)) {
neighbor = map.get(label);
} else {
neighbor = new UndirectedGraphNode(label);
map.put(label, neighbor);
}
if (node.neighbors == null) {
node.neighbors = new ArrayList<UndirectedGraphNode>();
}
if (neighbor.label == node.label) {
neighbor = new UndirectedGraphNode(label);
}
node.neighbors.add(neighbor);
}
}
}
return first;
}
public static void print (UndirectedGraphNode node) {
if (node == null) {
return;
}
StringBuffer buffer = new StringBuffer();
Queue<UndirectedGraphNode> queue = new ArrayDeque<UndirectedGraphNode>();
queue.offer(node);
while (queue.size() > 0) {
UndirectedGraphNode cur = queue.poll();
buffer.append(cur.label);
for (UndirectedGraphNode neighbor : cur.neighbors) {
buffer.append(",");
buffer.append(neighbor.label);
queue.offer(neighbor);
}
buffer.append("#");
}
if (buffer.length() > 0) {
buffer.deleteCharAt(buffer.length()-1);
}
System.out.println(buffer);
}
private static class UndirectedGraphNode {
int label;
List<UndirectedGraphNode> neighbors;
public UndirectedGraphNode(int label) {
this.label = label;
}
}
public static void main(String[] args) {
CloneGraph cloneGraph = new CloneGraph();
String graphStr = "0,1,2#1,2#2,2";
print(cloneGraph.clone(createGraph(graphStr)));
}
}
leetcode — clone-graph的更多相关文章
- LeetCode: Clone Graph 解题报告
Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [LeetCode] Clone Graph 克隆无向图
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...
- LeetCode:Clone Graph
题目如下:实现克隆图的算法 题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...
- [leetcode]Clone Graph @ Python
原题地址:https://oj.leetcode.com/problems/clone-graph/ 题意:实现对一个图的深拷贝. 解题思路:由于遍历一个图有两种方式:bfs和dfs.所以深拷贝一个图 ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- 【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 ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
随机推荐
- [Git]2018-10 解决git cmd中文乱码问题
2018年10月12日 莫名其妙出现cmd下git log中文乱码问题,显示一堆<E4><A8>之类的乱码.git bash却一切正常. 怀疑是Windows系统升级出现的不兼 ...
- FCC学习笔记(二)
Nest an Anchor Element within a Paragraph 作为参考,再次看一看a元素的图示: 例如: <p>Here's a <a href="h ...
- 针对Oracle用户被锁的一些相关处理方法
当登录时被告知XXX用户被锁时,可进行以下操作: 1.用拥有dba权限的用户登录,进行解锁,先设置具体时间格式,方便后面查看被锁的具体时间: SQL> alter session set nls ...
- /usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn't match a supported version! RequestsDependencyWarning)
[root@iZwz9bhan5nqzh979qokrkZ ~]# ansible all -m ping /usr/lib/python2.7/site-packages/requests/__in ...
- 机器学习(九)隐马尔可夫模型HMM
1.隐马尔可夫HMM模型 一个隐马尔可夫模型可以表示为\[\lambda=\{A,B,\pi\}\]具体就不说了,比较基本. 2.HMM模型的三个基本问题 1.概率计算问题:给定\(\lambda\) ...
- React组件和生命周期简介
React 简介----React 是 Facebook 出品的一套颠覆式的前端开发类库.为什么说它是颠覆式的呢? 内存维护虚拟 DOM 对于传统的 DOM 维护,我们的步骤可能是:1.初始化 ...
- Linux 管理进程
探查进程 参数 描述 -A 显示所有进程 -N 显示与指定参数不符的所有进程 -a 显示除控制进程(session leader1)和无终端进程外的所有进程 -d 显示除控制进程外的所有进程 -e 显 ...
- ubuntu18.04新体验
虽然ubuntu18.04LST版本早出来了,但自己原来的ubuntu16.04还可以用,就懒得折腾了. 但最近ubuntu崩了,就想尝尝鲜...结果发现还挺好用的,准确地说,ubuntu是越来越好用 ...
- 使用Bandwagon服务器ftp解决git clone速度慢的问题
写在前面 git clone速度往往很慢,我们可以先在身处美国的服务器上git clone,然后把文件用ftp传回来即可. 开始 我们以opencv为例 git clone https://githu ...
- 为什么导入本地jquery.js老是无效?(已解决)
我从jquery官网里复制过来jquery.js内容,然后粘贴到本地,但是引用的时候总是无效. 在翻看脚本所在目录,无意间发现脚本文件是个jquery.js.js, 原来是我的文件的扩展名的问题 ...