https://leetcode-cn.com/problems/clone-graph/
給你無向 連通 圖中一個節點的參照,請你返回該圖的 深拷貝(克隆)。
圖中的每個節點都包含它的值 val(int) 和其鄰居的列表(list[Node])。
class Node {
public int val;
public List neighbors;
}
測試用例格式:
簡單起見,每個節點的值都和它的索引相同。例如,第一個節點值爲 1(val = 1),第二個節點值爲 2(val = 2),以此類推。該圖在測試用例中使用鄰接列表表示。
鄰接列表 是用於表示有限圖的無序列表的集合。每個列表都描述了圖中節點的鄰居集。
給定節點將始終是圖中的第一個節點(值爲 1)。你必須將 給定節點的拷貝 作爲對克隆圖的參照返回。
cloneNodes
記錄已經存取、克隆過的Node,下標就是Node的val/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/
class Solution {
public Node cloneGraph(Node node) {
if(node == null){
return null;
}
Node[] cloneNodes = new Node[101];
Queue<Node> queue = new LinkedList<>();
Node cNode = new Node(node.val, (ArrayList<Node>)node.neighbors);
cloneNodes[cNode.val] = cNode;
queue.add(cNode);
while(queue.size() > 0){
Node cloneNode = queue.poll();
List<Node> cloneList = new ArrayList<>();
for(Node ne : cloneNode.neighbors){
if(cloneNodes[ne.val] == null){
ne = new Node(ne.val, (ArrayList<Node>)ne.neighbors);
queue.add(ne);
cloneNodes[ne.val] = ne;
} else{
ne = cloneNodes[ne.val];
}
cloneList.add(ne);
}
cloneNode.neighbors = cloneList;
}
return cNode;
}
}
O(N)
O(C)
cloneNodes
記錄已經存取、克隆過的Node,下標就是Node的val/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/
class Solution {
Node[] cloneNodes = new Node[101];
public Node cloneGraph(Node node) {
if(node == null){
return null;
}
return dfs(node);
}
private Node dfs(Node node){
// 優先從快取取
if(cloneNodes[node.val] != null){
return cloneNodes[node.val];
}
// 快取沒有就克隆一個
ArrayList<Node> cloneList = new ArrayList<>();
Node cNode = new Node(node.val, cloneList);
cloneNodes[node.val] = cNode;
for(Node ne : node.neighbors){
cloneList.add(dfs(ne));
}
return cNode;
}
}
O(N)
O(C)