diff --git a/solution/0100-0199/0133.Clone Graph/README.md b/solution/0100-0199/0133.Clone Graph/README.md index 68bcd02dffc94..50e374c99314d 100644 --- a/solution/0100-0199/0133.Clone Graph/README.md +++ b/solution/0100-0199/0133.Clone Graph/README.md @@ -92,7 +92,20 @@ class Node { -### 方法一 +### 方法一:哈希表 + DFS + +我们用一个哈希表 $\textit{g}$ 记录原图中的每个节点和它的拷贝节点之间的对应关系,然后进行深度优先搜索。 + +我们定义函数 $\text{dfs}(node)$,它的功能是返回 $\textit{node}$ 节点的拷贝节点。$\text{dfs}(node)$ 的过程如下: + +- 如果 $\textit{node}$ 是 $\text{null}$,那么 $\text{dfs}(node)$ 的返回值是 $\text{null}$。 +- 如果 $\textit{node}$ 在 $\textit{g}$ 中,那么 $\text{dfs}(node)$ 的返回值是 $\textit{g}[node]$。 +- 否则我们创建一个新的节点 $\textit{cloned}$,并将 $\textit{g}[node]$ 的值设为 $\textit{cloned}$,然后遍历 $\textit{node}$ 的所有邻居节点 $\textit{nxt}$,并将 $\textit{cloned}$ 的邻居节点列表中加入 $\text{dfs}(nxt)$。 +- 最后返回 $\textit{cloned}$。 + +在主函数中,我们返回 $\text{dfs}(node)$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是节点的数量。 @@ -107,23 +120,24 @@ class Node: self.neighbors = neighbors if neighbors is not None else [] """ +from typing import Optional -class Solution: - def cloneGraph(self, node: 'Node') -> 'Node': - visited = defaultdict() - def clone(node): +class Solution: + def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]: + def dfs(node): if node is None: return None - if node in visited: - return visited[node] - c = Node(node.val) - visited[node] = c - for e in node.neighbors: - c.neighbors.append(clone(e)) - return c - - return clone(node) + if node in g: + return g[node] + cloned = Node(node.val) + g[node] = cloned + for nxt in node.neighbors: + cloned.neighbors.append(dfs(nxt)) + return cloned + + g = defaultdict() + return dfs(node) ``` #### Java @@ -150,21 +164,25 @@ class Node { */ class Solution { - private Map visited = new HashMap<>(); + private Map g = new HashMap<>(); public Node cloneGraph(Node node) { + return dfs(node); + } + + private Node dfs(Node node) { if (node == null) { return null; } - if (visited.containsKey(node)) { - return visited.get(node); - } - Node clone = new Node(node.val); - visited.put(node, clone); - for (Node e : node.neighbors) { - clone.neighbors.add(cloneGraph(e)); + Node cloned = g.get(node); + if (cloned == null) { + cloned = new Node(node.val); + g.put(node, cloned); + for (Node nxt : node.neighbors) { + cloned.neighbors.add(dfs(nxt)); + } } - return clone; + return cloned; } } ``` @@ -195,16 +213,23 @@ public: class Solution { public: - unordered_map visited; - Node* cloneGraph(Node* node) { - if (!node) return nullptr; - if (visited.count(node)) return visited[node]; - Node* clone = new Node(node->val); - visited[node] = clone; - for (auto& e : node->neighbors) - clone->neighbors.push_back(cloneGraph(e)); - return clone; + unordered_map g; + auto dfs = [&](this auto&& dfs, Node* node) -> Node* { + if (!node) { + return nullptr; + } + if (g.contains(node)) { + return g[node]; + } + Node* cloned = new Node(node->val); + g[node] = cloned; + for (auto& nxt : node->neighbors) { + cloned->neighbors.push_back(dfs(nxt)); + } + return cloned; + }; + return dfs(node); } }; ``` @@ -221,24 +246,23 @@ public: */ func cloneGraph(node *Node) *Node { - visited := map[*Node]*Node{} - var clone func(node *Node) *Node - clone = func(node *Node) *Node { + g := map[*Node]*Node{} + var dfs func(node *Node) *Node + dfs = func(node *Node) *Node { if node == nil { return nil } - if _, ok := visited[node]; ok { - return visited[node] + if n, ok := g[node]; ok { + return n } - c := &Node{node.Val, []*Node{}} - visited[node] = c - for _, e := range node.Neighbors { - c.Neighbors = append(c.Neighbors, clone(e)) + cloned := &Node{node.Val, []*Node{}} + g[node] = cloned + for _, nxt := range node.Neighbors { + cloned.Neighbors = append(cloned.Neighbors, dfs(nxt)) } - return c + return cloned } - - return clone(node) + return dfs(node) } ``` @@ -246,74 +270,118 @@ func cloneGraph(node *Node) *Node { ```ts /** - * Definition for Node. - * class Node { + * Definition for _Node. + * class _Node { * val: number - * neighbors: Node[] - * constructor(val?: number, neighbors?: Node[]) { + * neighbors: _Node[] + * + * constructor(val?: number, neighbors?: _Node[]) { * this.val = (val===undefined ? 0 : val) * this.neighbors = (neighbors===undefined ? [] : neighbors) * } * } + * */ -function cloneGraph(node: Node | null): Node | null { - if (node == null) return null; - - const visited = new Map(); - visited.set(node, new Node(node.val)); - const queue = [node]; - while (queue.length) { - const cur = queue.shift(); - for (let neighbor of cur.neighbors || []) { - if (!visited.has(neighbor)) { - queue.push(neighbor); - const newNeighbor = new Node(neighbor.val, []); - visited.set(neighbor, newNeighbor); - } - const newNode = visited.get(cur); - newNode.neighbors.push(visited.get(neighbor)); +function cloneGraph(node: _Node | null): _Node | null { + const g: Map<_Node, _Node> = new Map(); + const dfs = (node: _Node | null): _Node | null => { + if (!node) { + return null; } - } - return visited.get(node); + if (g.has(node)) { + return g.get(node); + } + const cloned = new _Node(node.val); + g.set(node, cloned); + for (const nxt of node.neighbors) { + cloned.neighbors.push(dfs(nxt)); + } + return cloned; + }; + return dfs(node); } ``` +#### JavaScript + +```js +/** + * // Definition for a _Node. + * function _Node(val, neighbors) { + * this.val = val === undefined ? 0 : val; + * this.neighbors = neighbors === undefined ? [] : neighbors; + * }; + */ + +/** + * @param {_Node} node + * @return {_Node} + */ +var cloneGraph = function (node) { + const g = new Map(); + const dfs = node => { + if (!node) { + return null; + } + if (g.has(node)) { + return g.get(node); + } + const cloned = new _Node(node.val); + g.set(node, cloned); + for (const nxt of node.neighbors) { + cloned.neighbors.push(dfs(nxt)); + } + return cloned; + }; + return dfs(node); +}; +``` + #### C# ```cs -using System.Collections.Generic; +/* +// Definition for a Node. +public class Node { + public int val; + public IList neighbors; + + public Node() { + val = 0; + neighbors = new List(); + } + + public Node(int _val) { + val = _val; + neighbors = new List(); + } + + public Node(int _val, List _neighbors) { + val = _val; + neighbors = _neighbors; + } +} +*/ public class Solution { public Node CloneGraph(Node node) { - if (node == null) return null; - var dict = new Dictionary(); - var queue = new Queue(); - queue.Enqueue(CloneVal(node)); - dict.Add(node.val, queue.Peek()); - while (queue.Count > 0) - { - var current = queue.Dequeue(); - var newNeighbors = new List(current.neighbors.Count); - foreach (var oldNeighbor in current.neighbors) - { - Node newNeighbor; - if (!dict.TryGetValue(oldNeighbor.val, out newNeighbor)) - { - newNeighbor = CloneVal(oldNeighbor); - queue.Enqueue(newNeighbor); - dict.Add(newNeighbor.val, newNeighbor); - } - newNeighbors.Add(newNeighbor); + var g = new Dictionary(); + Node Dfs(Node n) { + if (n == null) { + return null; + } + if (g.ContainsKey(n)) { + return g[n]; } - current.neighbors = newNeighbors; + var cloned = new Node(n.val); + g[n] = cloned; + foreach (var neighbor in n.neighbors) { + cloned.neighbors.Add(Dfs(neighbor)); + } + return cloned; } - return dict[node.val]; - } - - private Node CloneVal(Node node) - { - return new Node(node.val, new List(node.neighbors)); + return Dfs(node); } } ``` diff --git a/solution/0100-0199/0133.Clone Graph/README_EN.md b/solution/0100-0199/0133.Clone Graph/README_EN.md index f64c6ebb729bf..dcefbefee33ab 100644 --- a/solution/0100-0199/0133.Clone Graph/README_EN.md +++ b/solution/0100-0199/0133.Clone Graph/README_EN.md @@ -88,7 +88,20 @@ class Node { -### Solution 1 +### Solution 1: Hash Table + DFS + +We use a hash table $\textit{g}$ to record the correspondence between each node in the original graph and its copy, and then perform depth-first search. + +We define the function $\text{dfs}(node)$, which returns the copy of the $\textit{node}$. The process of $\text{dfs}(node)$ is as follows: + +- If $\textit{node}$ is $\text{null}$, then the return value of $\text{dfs}(node)$ is $\text{null}$. +- If $\textit{node}$ is in $\textit{g}$, then the return value of $\text{dfs}(node)$ is $\textit{g}[node]$. +- Otherwise, we create a new node $\textit{cloned}$ and set the value of $\textit{g}[node]$ to $\textit{cloned}$. Then, we traverse all the neighbor nodes $\textit{nxt}$ of $\textit{node}$ and add $\text{dfs}(nxt)$ to the neighbor list of $\textit{cloned}$. +- Finally, return $\textit{cloned}$. + +In the main function, we return $\text{dfs}(node)$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes. @@ -103,23 +116,24 @@ class Node: self.neighbors = neighbors if neighbors is not None else [] """ +from typing import Optional -class Solution: - def cloneGraph(self, node: 'Node') -> 'Node': - visited = defaultdict() - def clone(node): +class Solution: + def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]: + def dfs(node): if node is None: return None - if node in visited: - return visited[node] - c = Node(node.val) - visited[node] = c - for e in node.neighbors: - c.neighbors.append(clone(e)) - return c - - return clone(node) + if node in g: + return g[node] + cloned = Node(node.val) + g[node] = cloned + for nxt in node.neighbors: + cloned.neighbors.append(dfs(nxt)) + return cloned + + g = defaultdict() + return dfs(node) ``` #### Java @@ -146,21 +160,25 @@ class Node { */ class Solution { - private Map visited = new HashMap<>(); + private Map g = new HashMap<>(); public Node cloneGraph(Node node) { + return dfs(node); + } + + private Node dfs(Node node) { if (node == null) { return null; } - if (visited.containsKey(node)) { - return visited.get(node); - } - Node clone = new Node(node.val); - visited.put(node, clone); - for (Node e : node.neighbors) { - clone.neighbors.add(cloneGraph(e)); + Node cloned = g.get(node); + if (cloned == null) { + cloned = new Node(node.val); + g.put(node, cloned); + for (Node nxt : node.neighbors) { + cloned.neighbors.add(dfs(nxt)); + } } - return clone; + return cloned; } } ``` @@ -191,16 +209,23 @@ public: class Solution { public: - unordered_map visited; - Node* cloneGraph(Node* node) { - if (!node) return nullptr; - if (visited.count(node)) return visited[node]; - Node* clone = new Node(node->val); - visited[node] = clone; - for (auto& e : node->neighbors) - clone->neighbors.push_back(cloneGraph(e)); - return clone; + unordered_map g; + auto dfs = [&](this auto&& dfs, Node* node) -> Node* { + if (!node) { + return nullptr; + } + if (g.contains(node)) { + return g[node]; + } + Node* cloned = new Node(node->val); + g[node] = cloned; + for (auto& nxt : node->neighbors) { + cloned->neighbors.push_back(dfs(nxt)); + } + return cloned; + }; + return dfs(node); } }; ``` @@ -217,24 +242,23 @@ public: */ func cloneGraph(node *Node) *Node { - visited := map[*Node]*Node{} - var clone func(node *Node) *Node - clone = func(node *Node) *Node { + g := map[*Node]*Node{} + var dfs func(node *Node) *Node + dfs = func(node *Node) *Node { if node == nil { return nil } - if _, ok := visited[node]; ok { - return visited[node] + if n, ok := g[node]; ok { + return n } - c := &Node{node.Val, []*Node{}} - visited[node] = c - for _, e := range node.Neighbors { - c.Neighbors = append(c.Neighbors, clone(e)) + cloned := &Node{node.Val, []*Node{}} + g[node] = cloned + for _, nxt := range node.Neighbors { + cloned.Neighbors = append(cloned.Neighbors, dfs(nxt)) } - return c + return cloned } - - return clone(node) + return dfs(node) } ``` @@ -242,74 +266,118 @@ func cloneGraph(node *Node) *Node { ```ts /** - * Definition for Node. - * class Node { + * Definition for _Node. + * class _Node { * val: number - * neighbors: Node[] - * constructor(val?: number, neighbors?: Node[]) { + * neighbors: _Node[] + * + * constructor(val?: number, neighbors?: _Node[]) { * this.val = (val===undefined ? 0 : val) * this.neighbors = (neighbors===undefined ? [] : neighbors) * } * } + * */ -function cloneGraph(node: Node | null): Node | null { - if (node == null) return null; - - const visited = new Map(); - visited.set(node, new Node(node.val)); - const queue = [node]; - while (queue.length) { - const cur = queue.shift(); - for (let neighbor of cur.neighbors || []) { - if (!visited.has(neighbor)) { - queue.push(neighbor); - const newNeighbor = new Node(neighbor.val, []); - visited.set(neighbor, newNeighbor); - } - const newNode = visited.get(cur); - newNode.neighbors.push(visited.get(neighbor)); +function cloneGraph(node: _Node | null): _Node | null { + const g: Map<_Node, _Node> = new Map(); + const dfs = (node: _Node | null): _Node | null => { + if (!node) { + return null; } - } - return visited.get(node); + if (g.has(node)) { + return g.get(node); + } + const cloned = new _Node(node.val); + g.set(node, cloned); + for (const nxt of node.neighbors) { + cloned.neighbors.push(dfs(nxt)); + } + return cloned; + }; + return dfs(node); } ``` +#### JavaScript + +```js +/** + * // Definition for a _Node. + * function _Node(val, neighbors) { + * this.val = val === undefined ? 0 : val; + * this.neighbors = neighbors === undefined ? [] : neighbors; + * }; + */ + +/** + * @param {_Node} node + * @return {_Node} + */ +var cloneGraph = function (node) { + const g = new Map(); + const dfs = node => { + if (!node) { + return null; + } + if (g.has(node)) { + return g.get(node); + } + const cloned = new _Node(node.val); + g.set(node, cloned); + for (const nxt of node.neighbors) { + cloned.neighbors.push(dfs(nxt)); + } + return cloned; + }; + return dfs(node); +}; +``` + #### C# ```cs -using System.Collections.Generic; +/* +// Definition for a Node. +public class Node { + public int val; + public IList neighbors; + + public Node() { + val = 0; + neighbors = new List(); + } + + public Node(int _val) { + val = _val; + neighbors = new List(); + } + + public Node(int _val, List _neighbors) { + val = _val; + neighbors = _neighbors; + } +} +*/ public class Solution { public Node CloneGraph(Node node) { - if (node == null) return null; - var dict = new Dictionary(); - var queue = new Queue(); - queue.Enqueue(CloneVal(node)); - dict.Add(node.val, queue.Peek()); - while (queue.Count > 0) - { - var current = queue.Dequeue(); - var newNeighbors = new List(current.neighbors.Count); - foreach (var oldNeighbor in current.neighbors) - { - Node newNeighbor; - if (!dict.TryGetValue(oldNeighbor.val, out newNeighbor)) - { - newNeighbor = CloneVal(oldNeighbor); - queue.Enqueue(newNeighbor); - dict.Add(newNeighbor.val, newNeighbor); - } - newNeighbors.Add(newNeighbor); + var g = new Dictionary(); + Node Dfs(Node n) { + if (n == null) { + return null; + } + if (g.ContainsKey(n)) { + return g[n]; } - current.neighbors = newNeighbors; + var cloned = new Node(n.val); + g[n] = cloned; + foreach (var neighbor in n.neighbors) { + cloned.neighbors.Add(Dfs(neighbor)); + } + return cloned; } - return dict[node.val]; - } - - private Node CloneVal(Node node) - { - return new Node(node.val, new List(node.neighbors)); + return Dfs(node); } } ``` diff --git a/solution/0100-0199/0133.Clone Graph/Solution.cpp b/solution/0100-0199/0133.Clone Graph/Solution.cpp index cfef4bd034149..85d549f56b1fb 100644 --- a/solution/0100-0199/0133.Clone Graph/Solution.cpp +++ b/solution/0100-0199/0133.Clone Graph/Solution.cpp @@ -21,15 +21,22 @@ class Node { class Solution { public: - unordered_map visited; - Node* cloneGraph(Node* node) { - if (!node) return nullptr; - if (visited.count(node)) return visited[node]; - Node* clone = new Node(node->val); - visited[node] = clone; - for (auto& e : node->neighbors) - clone->neighbors.push_back(cloneGraph(e)); - return clone; + unordered_map g; + auto dfs = [&](this auto&& dfs, Node* node) -> Node* { + if (!node) { + return nullptr; + } + if (g.contains(node)) { + return g[node]; + } + Node* cloned = new Node(node->val); + g[node] = cloned; + for (auto& nxt : node->neighbors) { + cloned->neighbors.push_back(dfs(nxt)); + } + return cloned; + }; + return dfs(node); } -}; \ No newline at end of file +}; diff --git a/solution/0100-0199/0133.Clone Graph/Solution.cs b/solution/0100-0199/0133.Clone Graph/Solution.cs index 9abef5a6044c3..24e5fc0a132f4 100644 --- a/solution/0100-0199/0133.Clone Graph/Solution.cs +++ b/solution/0100-0199/0133.Clone Graph/Solution.cs @@ -1,34 +1,43 @@ -using System.Collections.Generic; +/* +// Definition for a Node. +public class Node { + public int val; + public IList neighbors; + + public Node() { + val = 0; + neighbors = new List(); + } + + public Node(int _val) { + val = _val; + neighbors = new List(); + } + + public Node(int _val, List _neighbors) { + val = _val; + neighbors = _neighbors; + } +} +*/ public class Solution { public Node CloneGraph(Node node) { - if (node == null) return null; - var dict = new Dictionary(); - var queue = new Queue(); - queue.Enqueue(CloneVal(node)); - dict.Add(node.val, queue.Peek()); - while (queue.Count > 0) - { - var current = queue.Dequeue(); - var newNeighbors = new List(current.neighbors.Count); - foreach (var oldNeighbor in current.neighbors) - { - Node newNeighbor; - if (!dict.TryGetValue(oldNeighbor.val, out newNeighbor)) - { - newNeighbor = CloneVal(oldNeighbor); - queue.Enqueue(newNeighbor); - dict.Add(newNeighbor.val, newNeighbor); - } - newNeighbors.Add(newNeighbor); + var g = new Dictionary(); + Node Dfs(Node n) { + if (n == null) { + return null; + } + if (g.ContainsKey(n)) { + return g[n]; + } + var cloned = new Node(n.val); + g[n] = cloned; + foreach (var neighbor in n.neighbors) { + cloned.neighbors.Add(Dfs(neighbor)); } - current.neighbors = newNeighbors; + return cloned; } - return dict[node.val]; - } - - private Node CloneVal(Node node) - { - return new Node(node.val, new List(node.neighbors)); + return Dfs(node); } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0133.Clone Graph/Solution.go b/solution/0100-0199/0133.Clone Graph/Solution.go index 95bd357624546..410ce5c708f41 100644 --- a/solution/0100-0199/0133.Clone Graph/Solution.go +++ b/solution/0100-0199/0133.Clone Graph/Solution.go @@ -7,22 +7,21 @@ */ func cloneGraph(node *Node) *Node { - visited := map[*Node]*Node{} - var clone func(node *Node) *Node - clone = func(node *Node) *Node { + g := map[*Node]*Node{} + var dfs func(node *Node) *Node + dfs = func(node *Node) *Node { if node == nil { return nil } - if _, ok := visited[node]; ok { - return visited[node] + if n, ok := g[node]; ok { + return n } - c := &Node{node.Val, []*Node{}} - visited[node] = c - for _, e := range node.Neighbors { - c.Neighbors = append(c.Neighbors, clone(e)) + cloned := &Node{node.Val, []*Node{}} + g[node] = cloned + for _, nxt := range node.Neighbors { + cloned.Neighbors = append(cloned.Neighbors, dfs(nxt)) } - return c + return cloned } - - return clone(node) -} \ No newline at end of file + return dfs(node) +} diff --git a/solution/0100-0199/0133.Clone Graph/Solution.java b/solution/0100-0199/0133.Clone Graph/Solution.java index 8f2ae0bdc3974..b5cae79398665 100644 --- a/solution/0100-0199/0133.Clone Graph/Solution.java +++ b/solution/0100-0199/0133.Clone Graph/Solution.java @@ -19,20 +19,24 @@ public Node(int _val, ArrayList _neighbors) { */ class Solution { - private Map visited = new HashMap<>(); + private Map g = new HashMap<>(); public Node cloneGraph(Node node) { + return dfs(node); + } + + private Node dfs(Node node) { if (node == null) { return null; } - if (visited.containsKey(node)) { - return visited.get(node); + Node cloned = g.get(node); + if (cloned == null) { + cloned = new Node(node.val); + g.put(node, cloned); + for (Node nxt : node.neighbors) { + cloned.neighbors.add(dfs(nxt)); + } } - Node clone = new Node(node.val); - visited.put(node, clone); - for (Node e : node.neighbors) { - clone.neighbors.add(cloneGraph(e)); - } - return clone; + return cloned; } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0133.Clone Graph/Solution.js b/solution/0100-0199/0133.Clone Graph/Solution.js new file mode 100644 index 0000000000000..f0f7d53a072b3 --- /dev/null +++ b/solution/0100-0199/0133.Clone Graph/Solution.js @@ -0,0 +1,30 @@ +/** + * // Definition for a _Node. + * function _Node(val, neighbors) { + * this.val = val === undefined ? 0 : val; + * this.neighbors = neighbors === undefined ? [] : neighbors; + * }; + */ + +/** + * @param {_Node} node + * @return {_Node} + */ +var cloneGraph = function (node) { + const g = new Map(); + const dfs = node => { + if (!node) { + return null; + } + if (g.has(node)) { + return g.get(node); + } + const cloned = new _Node(node.val); + g.set(node, cloned); + for (const nxt of node.neighbors) { + cloned.neighbors.push(dfs(nxt)); + } + return cloned; + }; + return dfs(node); +}; diff --git a/solution/0100-0199/0133.Clone Graph/Solution.py b/solution/0100-0199/0133.Clone Graph/Solution.py index 366f123af1dae..0f2a0a533cd91 100644 --- a/solution/0100-0199/0133.Clone Graph/Solution.py +++ b/solution/0100-0199/0133.Clone Graph/Solution.py @@ -6,20 +6,21 @@ def __init__(self, val = 0, neighbors = None): self.neighbors = neighbors if neighbors is not None else [] """ +from typing import Optional -class Solution: - def cloneGraph(self, node: 'Node') -> 'Node': - visited = defaultdict() - def clone(node): +class Solution: + def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]: + def dfs(node): if node is None: return None - if node in visited: - return visited[node] - c = Node(node.val) - visited[node] = c - for e in node.neighbors: - c.neighbors.append(clone(e)) - return c + if node in g: + return g[node] + cloned = Node(node.val) + g[node] = cloned + for nxt in node.neighbors: + cloned.neighbors.append(dfs(nxt)) + return cloned - return clone(node) + g = defaultdict() + return dfs(node) diff --git a/solution/0100-0199/0133.Clone Graph/Solution.ts b/solution/0100-0199/0133.Clone Graph/Solution.ts index 59cadc616cf1d..782253d04aba2 100644 --- a/solution/0100-0199/0133.Clone Graph/Solution.ts +++ b/solution/0100-0199/0133.Clone Graph/Solution.ts @@ -1,32 +1,32 @@ /** - * Definition for Node. - * class Node { + * Definition for _Node. + * class _Node { * val: number - * neighbors: Node[] - * constructor(val?: number, neighbors?: Node[]) { + * neighbors: _Node[] + * + * constructor(val?: number, neighbors?: _Node[]) { * this.val = (val===undefined ? 0 : val) * this.neighbors = (neighbors===undefined ? [] : neighbors) * } * } + * */ -function cloneGraph(node: Node | null): Node | null { - if (node == null) return null; - - const visited = new Map(); - visited.set(node, new Node(node.val)); - const queue = [node]; - while (queue.length) { - const cur = queue.shift(); - for (let neighbor of cur.neighbors || []) { - if (!visited.has(neighbor)) { - queue.push(neighbor); - const newNeighbor = new Node(neighbor.val, []); - visited.set(neighbor, newNeighbor); - } - const newNode = visited.get(cur); - newNode.neighbors.push(visited.get(neighbor)); +function cloneGraph(node: _Node | null): _Node | null { + const g: Map<_Node, _Node> = new Map(); + const dfs = (node: _Node | null): _Node | null => { + if (!node) { + return null; + } + if (g.has(node)) { + return g.get(node); + } + const cloned = new _Node(node.val); + g.set(node, cloned); + for (const nxt of node.neighbors) { + cloned.neighbors.push(dfs(nxt)); } - } - return visited.get(node); + return cloned; + }; + return dfs(node); } diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/README.md b/solution/0500-0599/0532.K-diff Pairs in an Array/README.md index 6ec116976d429..1053a06cc32ac 100644 --- a/solution/0500-0599/0532.K-diff Pairs in an Array/README.md +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/README.md @@ -77,11 +77,13 @@ tags: ### 方法一:哈希表 -由于 $k$ 是一个定值,因此用哈希表 $ans$ 记录数对的较小值,就能够确定较大的值。最后返回 ans 的大小作为答案。 +由于 $k$ 是一个定值,我们可以用一个哈希表 $\textit{ans}$ 记录数对的较小值,就能够确定较大的值。最后返回 $\textit{ans}$ 的大小作为答案。 -遍历数组 $nums$,当前遍历到的数 $nums[j]$,我们记为 $v$,用哈希表 $vis$ 记录此前遍历到的所有数字。若 $v-k$ 在 $vis$ 中,则将 $v-k$ 添加至 $ans$;若 $v+k$ 在 $vis$ 中,则将 $v$ 添加至 $ans$。 +遍历数组 $\textit{nums}$,当前遍历到的数 $x$,我们用哈希表 $\textit{vis}$ 记录此前遍历到的所有数字。若 $x-k$ 在 $\textit{vis}$ 中,则将 $x-k$ 添加至 $\textit{ans}$;若 $x+k$ 在 $\textit{vis}$ 中,则将 $x$ 添加至 $\textit{ans}$。然后我们将 $x$ 添加至 $\textit{vis}$。继续遍历数组 $\textit{nums}$ 直至遍历结束。 -时间复杂度 $O(n)$,其中 $n$ 表示数组 $nums$ 的长度。 +最后返回 $\textit{ans}$ 的大小作为答案。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -90,13 +92,14 @@ tags: ```python class Solution: def findPairs(self, nums: List[int], k: int) -> int: - vis, ans = set(), set() - for v in nums: - if v - k in vis: - ans.add(v - k) - if v + k in vis: - ans.add(v) - vis.add(v) + ans = set() + vis = set() + for x in nums: + if x - k in vis: + ans.add(x - k) + if x + k in vis: + ans.add(x) + vis.add(x) return len(ans) ``` @@ -105,16 +108,16 @@ class Solution: ```java class Solution { public int findPairs(int[] nums, int k) { - Set vis = new HashSet<>(); Set ans = new HashSet<>(); - for (int v : nums) { - if (vis.contains(v - k)) { - ans.add(v - k); + Set vis = new HashSet<>(); + for (int x : nums) { + if (vis.contains(x - k)) { + ans.add(x - k); } - if (vis.contains(v + k)) { - ans.add(v); + if (vis.contains(x + k)) { + ans.add(x); } - vis.add(v); + vis.add(x); } return ans.size(); } @@ -127,12 +130,15 @@ class Solution { class Solution { public: int findPairs(vector& nums, int k) { - unordered_set vis; - unordered_set ans; - for (int& v : nums) { - if (vis.count(v - k)) ans.insert(v - k); - if (vis.count(v + k)) ans.insert(v); - vis.insert(v); + unordered_set ans, vis; + for (int x : nums) { + if (vis.count(x - k)) { + ans.insert(x - k); + } + if (vis.count(x + k)) { + ans.insert(x); + } + vis.insert(x); } return ans.size(); } @@ -143,52 +149,61 @@ public: ```go func findPairs(nums []int, k int) int { - vis := map[int]bool{} - ans := map[int]bool{} - for _, v := range nums { - if vis[v-k] { - ans[v-k] = true + ans := make(map[int]struct{}) + vis := make(map[int]struct{}) + + for _, x := range nums { + if _, ok := vis[x-k]; ok { + ans[x-k] = struct{}{} } - if vis[v+k] { - ans[v] = true + if _, ok := vis[x+k]; ok { + ans[x] = struct{}{} } - vis[v] = true + vis[x] = struct{}{} } return len(ans) } ``` +#### TypeScript + +```ts +function findPairs(nums: number[], k: number): number { + const ans = new Set(); + const vis = new Set(); + for (const x of nums) { + if (vis.has(x - k)) { + ans.add(x - k); + } + if (vis.has(x + k)) { + ans.add(x); + } + vis.add(x); + } + return ans.size; +} +``` + #### Rust ```rust +use std::collections::HashSet; + impl Solution { - pub fn find_pairs(mut nums: Vec, k: i32) -> i32 { - nums.sort(); - let n = nums.len(); - let mut res = 0; - let mut left = 0; - let mut right = 1; - while right < n { - let num = i32::abs(nums[left] - nums[right]); - if num == k { - res += 1; + pub fn find_pairs(nums: Vec, k: i32) -> i32 { + let mut ans = HashSet::new(); + let mut vis = HashSet::new(); + + for &x in &nums { + if vis.contains(&(x - k)) { + ans.insert(x - k); } - if num <= k { - right += 1; - while right < n && nums[right - 1] == nums[right] { - right += 1; - } - } else { - left += 1; - while left < right && nums[left - 1] == nums[left] { - left += 1; - } - if left == right { - right += 1; - } + if vis.contains(&(x + k)) { + ans.insert(x); } + vis.insert(x); } - res + ans.len() as i32 } } ``` @@ -197,16 +212,4 @@ impl Solution { - - -### 方法二:排序 + 双指针 - -只需要统计组合的数量,因此可以改动原数组,对其排序,使用双指针来统计。 - -声明 `left` 与 `right` 指针,初始化为 0 和 1。根据 `abs(nums[left] - nums[right])` 与 `k` 值对比结果移动指针。 - -需要注意的是,**不能出现重复的组合**,所以移动指针时,不能仅仅是 `+1`,需要到一个不等于当前值的位置。 - - - diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/README_EN.md b/solution/0500-0599/0532.K-diff Pairs in an Array/README_EN.md index df31876b15ab2..a7103510c9c72 100644 --- a/solution/0500-0599/0532.K-diff Pairs in an Array/README_EN.md +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/README_EN.md @@ -73,7 +73,15 @@ Although we have two 1s in the input, we should only return the number of -### Solution 1 +### Solution 1: Hash Table + +Since $k$ is a fixed value, we can use a hash table $\textit{ans}$ to record the smaller value of the pairs, which allows us to determine the larger value. Finally, we return the size of $\textit{ans}$ as the answer. + +We traverse the array $\textit{nums}$. For the current number $x$, we use a hash table $\textit{vis}$ to record all the numbers that have been traversed. If $x-k$ is in $\textit{vis}$, we add $x-k$ to $\textit{ans}$. If $x+k$ is in $\textit{vis}$, we add $x$ to $\textit{ans}$. Then, we add $x$ to $\textit{vis}$. Continue traversing the array $\textit{nums}$ until the end. + +Finally, we return the size of $\textit{ans}$ as the answer. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -82,13 +90,14 @@ Although we have two 1s in the input, we should only return the number of int: - vis, ans = set(), set() - for v in nums: - if v - k in vis: - ans.add(v - k) - if v + k in vis: - ans.add(v) - vis.add(v) + ans = set() + vis = set() + for x in nums: + if x - k in vis: + ans.add(x - k) + if x + k in vis: + ans.add(x) + vis.add(x) return len(ans) ``` @@ -97,16 +106,16 @@ class Solution: ```java class Solution { public int findPairs(int[] nums, int k) { - Set vis = new HashSet<>(); Set ans = new HashSet<>(); - for (int v : nums) { - if (vis.contains(v - k)) { - ans.add(v - k); + Set vis = new HashSet<>(); + for (int x : nums) { + if (vis.contains(x - k)) { + ans.add(x - k); } - if (vis.contains(v + k)) { - ans.add(v); + if (vis.contains(x + k)) { + ans.add(x); } - vis.add(v); + vis.add(x); } return ans.size(); } @@ -119,12 +128,15 @@ class Solution { class Solution { public: int findPairs(vector& nums, int k) { - unordered_set vis; - unordered_set ans; - for (int& v : nums) { - if (vis.count(v - k)) ans.insert(v - k); - if (vis.count(v + k)) ans.insert(v); - vis.insert(v); + unordered_set ans, vis; + for (int x : nums) { + if (vis.count(x - k)) { + ans.insert(x - k); + } + if (vis.count(x + k)) { + ans.insert(x); + } + vis.insert(x); } return ans.size(); } @@ -135,52 +147,61 @@ public: ```go func findPairs(nums []int, k int) int { - vis := map[int]bool{} - ans := map[int]bool{} - for _, v := range nums { - if vis[v-k] { - ans[v-k] = true + ans := make(map[int]struct{}) + vis := make(map[int]struct{}) + + for _, x := range nums { + if _, ok := vis[x-k]; ok { + ans[x-k] = struct{}{} } - if vis[v+k] { - ans[v] = true + if _, ok := vis[x+k]; ok { + ans[x] = struct{}{} } - vis[v] = true + vis[x] = struct{}{} } return len(ans) } ``` +#### TypeScript + +```ts +function findPairs(nums: number[], k: number): number { + const ans = new Set(); + const vis = new Set(); + for (const x of nums) { + if (vis.has(x - k)) { + ans.add(x - k); + } + if (vis.has(x + k)) { + ans.add(x); + } + vis.add(x); + } + return ans.size; +} +``` + #### Rust ```rust +use std::collections::HashSet; + impl Solution { - pub fn find_pairs(mut nums: Vec, k: i32) -> i32 { - nums.sort(); - let n = nums.len(); - let mut res = 0; - let mut left = 0; - let mut right = 1; - while right < n { - let num = i32::abs(nums[left] - nums[right]); - if num == k { - res += 1; + pub fn find_pairs(nums: Vec, k: i32) -> i32 { + let mut ans = HashSet::new(); + let mut vis = HashSet::new(); + + for &x in &nums { + if vis.contains(&(x - k)) { + ans.insert(x - k); } - if num <= k { - right += 1; - while right < n && nums[right - 1] == nums[right] { - right += 1; - } - } else { - left += 1; - while left < right && nums[left - 1] == nums[left] { - left += 1; - } - if left == right { - right += 1; - } + if vis.contains(&(x + k)) { + ans.insert(x); } + vis.insert(x); } - res + ans.len() as i32 } } ``` diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.cpp b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.cpp index 63a75d171b756..97132b90ddc92 100644 --- a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.cpp +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.cpp @@ -1,13 +1,16 @@ class Solution { public: int findPairs(vector& nums, int k) { - unordered_set vis; - unordered_set ans; - for (int& v : nums) { - if (vis.count(v - k)) ans.insert(v - k); - if (vis.count(v + k)) ans.insert(v); - vis.insert(v); + unordered_set ans, vis; + for (int x : nums) { + if (vis.count(x - k)) { + ans.insert(x - k); + } + if (vis.count(x + k)) { + ans.insert(x); + } + vis.insert(x); } return ans.size(); } -}; \ No newline at end of file +}; diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.go b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.go index d54f2e347e780..065fa1e420e81 100644 --- a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.go +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.go @@ -1,14 +1,15 @@ func findPairs(nums []int, k int) int { - vis := map[int]bool{} - ans := map[int]bool{} - for _, v := range nums { - if vis[v-k] { - ans[v-k] = true + ans := make(map[int]struct{}) + vis := make(map[int]struct{}) + + for _, x := range nums { + if _, ok := vis[x-k]; ok { + ans[x-k] = struct{}{} } - if vis[v+k] { - ans[v] = true + if _, ok := vis[x+k]; ok { + ans[x] = struct{}{} } - vis[v] = true + vis[x] = struct{}{} } return len(ans) -} \ No newline at end of file +} diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.java b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.java index afd1420fa631d..23b97854299c3 100644 --- a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.java +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.java @@ -1,16 +1,16 @@ class Solution { public int findPairs(int[] nums, int k) { - Set vis = new HashSet<>(); Set ans = new HashSet<>(); - for (int v : nums) { - if (vis.contains(v - k)) { - ans.add(v - k); + Set vis = new HashSet<>(); + for (int x : nums) { + if (vis.contains(x - k)) { + ans.add(x - k); } - if (vis.contains(v + k)) { - ans.add(v); + if (vis.contains(x + k)) { + ans.add(x); } - vis.add(v); + vis.add(x); } return ans.size(); } -} \ No newline at end of file +} diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.py b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.py index 8692434342cb1..2f14875c4ed39 100644 --- a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.py +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.py @@ -1,10 +1,11 @@ class Solution: def findPairs(self, nums: List[int], k: int) -> int: - vis, ans = set(), set() - for v in nums: - if v - k in vis: - ans.add(v - k) - if v + k in vis: - ans.add(v) - vis.add(v) + ans = set() + vis = set() + for x in nums: + if x - k in vis: + ans.add(x - k) + if x + k in vis: + ans.add(x) + vis.add(x) return len(ans) diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.rs b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.rs index 6ee3845d36594..ea6651392f489 100644 --- a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.rs +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.rs @@ -1,30 +1,19 @@ +use std::collections::HashSet; + impl Solution { - pub fn find_pairs(mut nums: Vec, k: i32) -> i32 { - nums.sort(); - let n = nums.len(); - let mut res = 0; - let mut left = 0; - let mut right = 1; - while right < n { - let num = i32::abs(nums[left] - nums[right]); - if num == k { - res += 1; + pub fn find_pairs(nums: Vec, k: i32) -> i32 { + let mut ans = HashSet::new(); + let mut vis = HashSet::new(); + + for &x in &nums { + if vis.contains(&(x - k)) { + ans.insert(x - k); } - if num <= k { - right += 1; - while right < n && nums[right - 1] == nums[right] { - right += 1; - } - } else { - left += 1; - while left < right && nums[left - 1] == nums[left] { - left += 1; - } - if left == right { - right += 1; - } + if vis.contains(&(x + k)) { + ans.insert(x); } + vis.insert(x); } - res + ans.len() as i32 } } diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.ts b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.ts new file mode 100644 index 0000000000000..9081b68088cf0 --- /dev/null +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.ts @@ -0,0 +1,14 @@ +function findPairs(nums: number[], k: number): number { + const ans = new Set(); + const vis = new Set(); + for (const x of nums) { + if (vis.has(x - k)) { + ans.add(x - k); + } + if (vis.has(x + k)) { + ans.add(x); + } + vis.add(x); + } + return ans.size; +}