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 ...
随机推荐
- Flume+Kafka+Storm整合
Flume+Kafka+Storm整合 1. 需求: 有一个客户端Client可以产生日志信息,我们需要通过Flume获取日志信息,再把该日志信息放入到Kafka的一个Topic:flume-to-k ...
- AES加密算法详解
AES 是一个对称密码分组算法,分组长度为128bit,密钥长度为128.192 和 256 bit. 整个加密过程如下图所示. 1.密钥生成算法 密钥扩展过程: 1) 将种子密钥按下图所示的格式排 ...
- VS2017 生成事件去除未修改项目
1.右键“解决方案”→“配置管理器” 2.列“生成”,反勾选无需编译的项目 3.点击“确定”,重新编译即可跳过未勾选的项目.
- 机器学习(六)K-means聚类、密度聚类、层次聚类、谱聚类
本文主要简述聚类算法族.聚类算法与前面文章的算法不同,它们属于非监督学习. 1.K-means聚类 记k个簇中心,为\(\mu_{1}\),\(\mu_{2}\),...,\(\mu_{k}\),每个 ...
- 排查MongoDB CPU使用率高的问题
1.公司业务调整,把一部分数据由Redis转至MongoDB,业务在测试环境正常,生产环境上线后发现压力一上来MongoDB的服务直接把CPU占满了,和开发的同学分析了一下也参考了一下百度上类似的问题 ...
- NPOI 修改指定单元格字体颜色
//创建一个字体颜色 IFont font = hssfworkbook.CreateFont(); //红色 font.Color = HSSFColor.Red.Index; //样式 ICell ...
- Web测试入门:Selenium+Chrome+Python+Mac OS
一.环境配置 Chromedriver 下载及环境配置 url:使用WebDriver在Chrome浏览器上进行测试时,需要从http://chromedriver.storage.googleapi ...
- kali linux 网络渗透测试学习笔记(二)OWASP ZAP工具扫描SQL injection漏洞失败
按照惯例,利用OWASP ZAP工具扫描SQL injection漏洞时,应该很快就可以扫描出来,但是在笔者进行扫描的时候,却遇到了以下状况: 这说明了该工具根本就没能够扫描出SQL注入的漏洞,不知道 ...
- [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- python之pickle模块
1 概念 pickle是python语言的标准模块,安装python后以包含pickle库,不需要再单独安装. pickle提供了一种简单的持久化功能,可以将对象以文件的形式存放在磁盘上. pickl ...