C++實(shí)現(xiàn)LeetCode(133.克隆無向圖)
[LeetCode] 133. Clone Graph 克隆無向圖
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.
Example:
Input:
{"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1}
Explanation:
Node 1's value is 1, and it has two neighbors: Node 2 and 4.
Node 2's value is 2, and it has two neighbors: Node 1 and 3.
Node 3's value is 3, and it has two neighbors: Node 2 and 4.
Node 4's value is 4, and it has two neighbors: Node 1 and 3.
Note:
- The number of nodes will be between 1 and 100.
- The undirected graph is a simple graph, which means no repeated edges and no self-loops in the graph.
- Since the graph is undirected, if node p has node q as neighbor, then node q must have node p as neighbor too.
- You must return the copy of the given node as a reference to the cloned graph.
這道無向圖的復(fù)制問題和之前的 Copy List with Random Pointer 有些類似,那道題的難點(diǎn)是如何處理每個(gè)結(jié)點(diǎn)的隨機(jī)指針,這道題目的難點(diǎn)在于如何處理每個(gè)結(jié)點(diǎn)的 neighbors,由于在深度拷貝每一個(gè)結(jié)點(diǎn)后,還要將其所有 neighbors 放到一個(gè) vector 中,而如何避免重復(fù)拷貝呢?這道題好就好在所有結(jié)點(diǎn)值不同,所以我們可以使用 HashMap 來對(duì)應(yīng)原圖中的結(jié)點(diǎn)和新生成的克隆圖中的結(jié)點(diǎn)。對(duì)于圖的遍歷的兩大基本方法是深度優(yōu)先搜索 DFS 和廣度優(yōu)先搜索 BFS,這里我們先使用深度優(yōu)先搜索DFS來解答此題,在遞歸函數(shù)中,首先判空,然后再看當(dāng)前的結(jié)點(diǎn)是否已經(jīng)被克隆過了,若在 HashMap 中存在,則直接返回其映射結(jié)點(diǎn)。否則就克隆當(dāng)前結(jié)點(diǎn),并在 HashMap 中建立映射,然后遍歷當(dāng)前結(jié)點(diǎn)的所有 neihbor 結(jié)點(diǎn),調(diào)用遞歸函數(shù)并且加到克隆結(jié)點(diǎn)的 neighbors 數(shù)組中即可,代碼如下:
解法一:
class Solution { public: Node* cloneGraph(Node* node) { unordered_map<Node*, Node*> m; return helper(node, m); } Node* helper(Node* node, unordered_map<Node*, Node*>& m) { if (!node) return NULL; if (m.count(node)) return m[node]; Node *clone = new Node(node->val); m[node] = clone; for (Node *neighbor : node->neighbors) { clone->neighbors.push_back(helper(neighbor, m)); } return clone; } };
我們也可以使用 BFS 來遍歷圖,使用隊(duì)列 queue 進(jìn)行輔助,還是需要一個(gè) HashMap 來建立原圖結(jié)點(diǎn)和克隆結(jié)點(diǎn)之間的映射。先克隆當(dāng)前結(jié)點(diǎn),然后建立映射,并加入 queue 中,進(jìn)行 while 循環(huán)。在循環(huán)中,取出隊(duì)首結(jié)點(diǎn),遍歷其所有 neighbor 結(jié)點(diǎn),若不在 HashMap 中,我們根據(jù) neigbor 結(jié)點(diǎn)值克隆一個(gè)新 neighbor 結(jié)點(diǎn),建立映射,并且排入 queue 中。然后將 neighbor 結(jié)點(diǎn)在 HashMap 中的映射結(jié)點(diǎn)加入到克隆結(jié)點(diǎn)的 neighbors 數(shù)組中即可,參見代碼如下:
解法二:
class Solution { public: Node* cloneGraph(Node* node) { if (!node) return NULL; unordered_map<Node*, Node*> m; queue<Node*> q{{node}}; Node *clone = new Node(node->val); m[node] = clone; while (!q.empty()) { Node *t = q.front(); q.pop(); for (Node *neighbor : t->neighbors) { if (!m.count(neighbor)) { m[neighbor] = new Node(neighbor->val); q.push(neighbor); } m[t]->neighbors.push_back(m[neighbor]); } } return clone; } };
類似題目:
參考資料:
https://leetcode.com/problems/clone-graph/
https://leetcode.com/problems/clone-graph/discuss/42313/C%2B%2B-BFSDFS
https://leetcode.com/problems/clone-graph/discuss/42309/Depth-First-Simple-Java-Solution
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(133.克隆無向圖)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)克隆無向圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(6.字型轉(zhuǎn)換字符串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(6.字型轉(zhuǎn)換字符串),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Cocos2d-x中獲取系統(tǒng)時(shí)間和隨機(jī)數(shù)實(shí)例
這篇文章主要介紹了Cocos2d-x中獲取系統(tǒng)時(shí)間和隨機(jī)數(shù)實(shí)例,本文代碼含有大量注釋來講解獲取系統(tǒng)時(shí)間和隨機(jī)數(shù)的方法,需要的朋友可以參考下2014-09-09C語言中g(shù)etchar和putchar的使用方法詳解
我們知道scanf函數(shù)可以從鍵盤輸入信息,而printf則可以輸出信息,同樣地,getchar和putchar也有同樣的功能,下面我來給大家介紹putchar和getchar的使用方法,需要的朋友可以參考下2023-08-08