C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
PHP
TypeScript
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/
class Solution {
public:
Node* cloneGraph(Node* node) {
}
};
运行代码
提交
python3 解法, 执行用时: 44 ms, 内存消耗: 15.3 MB, 提交时间: 2022-11-26 17:01:27
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""
from collections import deque
class Solution(object):
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
if not node:
return node
visited = {}
# 将题目给定的节点添加到队列
queue = deque([node])
# 克隆第一个节点并存储到哈希表中
visited[node] = Node(node.val, [])
# 广度优先搜索
while queue:
# 取出队列的头节点
n = queue.popleft()
# 遍历该节点的邻居
for neighbor in n.neighbors:
if neighbor not in visited:
# 如果没有被访问过,就克隆并存储在哈希表中
visited[neighbor] = Node(neighbor.val, [])
# 将邻居节点加入队列中
queue.append(neighbor)
# 更新当前节点的邻居列表
visited[n].neighbors.append(visited[neighbor])
return visited[node]
golang 解法, 执行用时: 4 ms, 内存消耗: 2.7 MB, 提交时间: 2022-11-26 17:01:13
/**
* Definition for a Node.
* type Node struct {
* Val int
* Neighbors []*Node
* }
*/
func cloneGraph(node *Node) *Node {
if node == nil {
return node
}
visited := map[*Node]*Node{}
// 将题目给定的节点添加到队列
queue := []*Node{node}
// 克隆第一个节点并存储到哈希表中
visited[node] = &Node{node.Val, []*Node{}}
// 广度优先搜索
for len(queue) > 0 {
// 取出队列的头节点
n := queue[0]
// 遍历该节点的邻居
queue = queue[1:]
for _, neighbor := range n.Neighbors {
if _, ok := visited[neighbor]; !ok {
// 如果没有被访问过,就克隆并存储在哈希表中
visited[neighbor] = &Node{neighbor.Val, []*Node{}}
// 将邻居节点加入队列中
queue = append(queue, neighbor)
}
// 更新当前节点的邻居列表
visited[n].Neighbors = append(visited[n].Neighbors, visited[neighbor])
}
}
return visited[node]
}
golang 解法, 执行用时: 0 ms, 内存消耗: 2.7 MB, 提交时间: 2022-11-26 17:00:57
/**
* Definition for a Node.
* type Node struct {
* Val int
* Neighbors []*Node
* }
*/
func cloneGraph(node *Node) *Node {
visited := map[*Node]*Node{}
var cg func(node *Node) *Node
cg = func(node *Node) *Node {
if node == nil {
return node
}
// 如果该节点已经被访问过了,则直接从哈希表中取出对应的克隆节点返回
if _, ok := visited[node]; ok {
return visited[node]
}
// 克隆节点,注意到为了深拷贝我们不会克隆它的邻居的列表
cloneNode := &Node{node.Val, []*Node{}}
// 哈希表存储
visited[node] = cloneNode
// 遍历该节点的邻居并更新克隆节点的邻居列表
for _, n := range node.Neighbors {
cloneNode.Neighbors = append(cloneNode.Neighbors, cg(n))
}
return cloneNode
}
return cg(node)
}
python3 解法, 执行用时: 44 ms, 内存消耗: 15.2 MB, 提交时间: 2022-11-26 17:00:38
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""
class Solution(object):
def __init__(self):
self.visited = {}
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
if not node:
return node
# 如果该节点已经被访问过了,则直接从哈希表中取出对应的克隆节点返回
if node in self.visited:
return self.visited[node]
# 克隆节点,注意到为了深拷贝我们不会克隆它的邻居的列表
clone_node = Node(node.val, [])
# 哈希表存储
self.visited[node] = clone_node
# 遍历该节点的邻居并更新克隆节点的邻居列表
if node.neighbors:
clone_node.neighbors = [self.cloneGraph(n) for n in node.neighbors]
return clone_node