LeetCode(133)Clone a Graph

本文介绍了一种无向图的克隆算法实现,重点在于使用哈希表存储原始节点与复制节点之间的映射关系,并利用队列进行广度优先搜索以确保所有相连节点都被正确复制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目如下:

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
       1
      / \
     /   \
    0 --- 2
             \
              3

分析如下:

本题没什么算法,写的时候仔细就可以了。
基本思路和这道题目《 LeetCode刷题(138)Copy List with Random Pointer》是一致的。要用一个hash表来存储老图的node*和新图的node*的对应关系。
写完了想想一些edge cases and corner cases是否能够跑通过, 比如输入node==NULL,比如输入的graph只有一个的node,没别的node也没有边。

我的代码:

struct UndirectedGraphNode {
   int label;
   vector<UndirectedGraphNode *> neighbors;
   UndirectedGraphNode(int x) : label(x) {};
};

class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if(node==NULL)
            return NULL;
        queue<UndirectedGraphNode*> cur_queue;
        unordered_map<UndirectedGraphNode*,UndirectedGraphNode * > node_map;
        UndirectedGraphNode* head_node=new UndirectedGraphNode(node->label);
        cur_queue.push(node);
        node_map[node]=head_node;
        while(!cur_queue.empty()){
            UndirectedGraphNode* cur_node=cur_queue.front();
            cur_queue.pop();
            for(int i=0;i<cur_node->neighbors.size();i++){
                UndirectedGraphNode* neighbors_node=cur_node->neighbors[i];
                if(node_map.find(neighbors_node)==node_map.end()){
                    UndirectedGraphNode* clone_neighbors_node=new UndirectedGraphNode(neighbors_node->label);
                    node_map[cur_node]->neighbors.push_back(clone_neighbors_node);
                    node_map[neighbors_node]=clone_neighbors_node;
                    cur_queue.push(neighbors_node);
                }else{
                    node_map[cur_node]->neighbors.push_back(node_map[neighbors_node]);
                }
            }
        }
        return head_node;
    }
};


update: 2014-12-10

重写了一下,发现这里很容易写错。

错误:    cpy_current->neighbors[i] = cur; // cpy_current是新建的graph的当前节点,它的vector变量 neighbors并没有在初始化时指定大小,所以不能取下标,需要push_back.
正确: cpy_current->neighbors.push_back(cur);

/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        std::queue<UndirectedGraphNode*> que;
        std::map<UndirectedGraphNode*, UndirectedGraphNode*> mp;
        UndirectedGraphNode *org_current, *cpy_current, *cur;
        if (node == NULL) return node;
        que.push(node);
        cpy_current = new UndirectedGraphNode(node->label);
        mp[node] = cpy_current;
        while(!que.empty()) {
            org_current = que.front();
            cpy_current = mp[org_current];
            que.pop();
            for ( int i =0; i < org_current->neighbors.size(); ++i) {
                if (mp.find(org_current->neighbors[i]) == mp.end()) {
                    cur = new UndirectedGraphNode(org_current->neighbors[i]->label);
                    mp[org_current->neighbors[i]] = cur;
                    //cpy_current->neighbors[i] = cur;
                    cpy_current->neighbors.push_back(cur);
                    que.push(org_current->neighbors[i]);
                } else {
                    cpy_current->neighbors.push_back(mp[org_current->neighbors[i]]);
                }
            }
        }
        return mp[node];
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值