首页 技术 正文
技术 2022年11月10日
0 收藏 642 点赞 3,762 浏览 3401 个字
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)));
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,957
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,480
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,327
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,110
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,742
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,776