溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++中怎么利用LeetCode克隆無向圖

發(fā)布時(shí)間:2021-07-28 17:39:13 來源:億速云 閱讀:156 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)C++中怎么利用LeetCode克隆無向圖,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

[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:

C++中怎么利用LeetCode克隆無向圖

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:

  1. The number of nodes will be between 1 and 100.

  2. The undirected graph is a simple graph, which means no repeated edges and no self-loops in the graph.

  3. Since the graph is undirected, if node p has node q as neighbor, then node q must have node p as neighbor too.

  4. 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 來對應(yīng)原圖中的結(jié)點(diǎn)和新生成的克隆圖中的結(jié)點(diǎn)。對于圖的遍歷的兩大基本方法是深度優(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;
    }
};

上述就是小編為大家分享的C++中怎么利用LeetCode克隆無向圖了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI