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 ...
随机推荐
- 操作redis数据库 & 操作Excel & 开发接口
操作redis数据库: string类型 1. 增 set,传俩个参数 key value(只要是字符串就行)2. 删 delete 传一个参数 key3. 修改 set 在目标key重新传参 key ...
- Hive参数的临时设置和永久性设置
Hive中有一些参数是系统给提供给用户的,我们可以通过这些参数的设置可以让Hive在不同的模式下工作,或者改变显示的效果. 1.通过set对参数值进行设定,这种设置只能是在本次会话有效,退出Hive就 ...
- hive动态分区和混合分区
各位看官,今天我们来讨论下再Hive中的动态分区和混合分区方面的一些知识点以及相关的一些问题. 前面我们已经讲过管理表和外部表的一般分区的一些知识点,对于需要对表创建很多的分区,那么用户就需要些很多的 ...
- 使用mongodb的一些笔记
show dbs # 从结果中发现有cmb_demo_23_hackeruse cmb_demo_23_hacker db.all_in_one.find({"_id":15480 ...
- 如何把if-else代码重构成高质量代码
原文:https://blog.csdn.net/qq_35440678/article/details/77939999 本文提纲: 为什么我们写的代码都是if-else? 这样的代码有什么缺点? ...
- Vue(三十)公共组件
以 分页 组件为例:(根据自己具体业务编写) 1.pagination.vue <template> <!-- 分页 --> <div class="table ...
- Round #3
题源:感谢 by hzwer 水灾(sliker.cpp/c/pas) 1000MS 64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的 ...
- 一些 NSArray 的基本操作代码例子
一些 NSArray 的基本操作代码例子 数组可以说是软件开发人员每天都要面对的基本操作,下面就分享一些 NSArray 的基本操作代码例子供苹果开发初学者参考,每段代码第一行会以注释方式说明该段代码 ...
- 小米 OJ 编程比赛 01 月常规赛_灯_找规律
灯 序号:#125难度:有挑战时间限制:1000ms内存限制:32M 描述 一个屋子有 n 个开关控制着 n 盏灯,但奇怪的是,每个开关对应的不是一盏灯,而是 n-1 盏灯,每次按下这个开关,其对应 ...
- koa 写简单服务
这两天用koa写了点服务,这里面和express还是有部分区别的 1.静态服务: koa 中,是有中间件, koa-static, const static_f = require('koa-sta ...