美文网首页
133. Clone Graph 复制无向图

133. Clone Graph 复制无向图

作者: 刘小小gogo | 来源:发表于2018-09-23 18:08 被阅读0次
image.png

bfs

/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if(!node) return NULL;
        map<UndirectedGraphNode *, UndirectedGraphNode *> hashmap;
        UndirectedGraphNode * p2 = new UndirectedGraphNode(node->label);
        queue<UndirectedGraphNode *> q;
        q.push(node);
        hashmap[node] = p2;
        while(!q.empty()){
            UndirectedGraphNode * cur = q.front();
            q.pop();
            p2 = hashmap[cur];
            for(int i = 0; i < cur->neighbors.size(); i++){
                UndirectedGraphNode * nb= cur->neighbors[i];
                if(hashmap.count(nb)) p2->neighbors.push_back(hashmap[nb]);
                else{
                    UndirectedGraphNode * tmp = new UndirectedGraphNode(nb->label);
                    hashmap[nb] = tmp;
                    p2->neighbors.push_back(hashmap[nb]);
                    q.push(nb);//要注意这里q的元素,是原图中的元素
                }
            }
        }
        return hashmap[node];
    }
};

dfs

/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if(!node) return NULL;
        map<UndirectedGraphNode *, UndirectedGraphNode *> hashmap;
        UndirectedGraphNode * p2 = new UndirectedGraphNode(node->label);
        stack<UndirectedGraphNode *> q;
        q.push(node);
        hashmap[node] = p2;
        while(!q.empty()){
            UndirectedGraphNode * cur = q.top();
            q.pop();
            p2 = hashmap[cur];
            for(int i = 0; i < cur->neighbors.size(); i++){
                UndirectedGraphNode * nb= cur->neighbors[i];
                if(hashmap.count(nb)) p2->neighbors.push_back(hashmap[nb]);
                else{
                    UndirectedGraphNode * tmp = new UndirectedGraphNode(nb->label);
                    hashmap[nb] = tmp;
                    p2->neighbors.push_back(hashmap[nb]);
                    q.push(nb);//要注意这里q的元素,是原图中的元素
                }
            }
        }
        return hashmap[node];
    }
};

相关文章

  • 133. Clone Graph 复制无向图

    bfs dfs

  • Leetcode图

    133. 克隆图[https://leetcode-cn.com/problems/clone-graph/] 2...

  • 133. Clone Graph

    第一步加到map里面value是null只是为了去重,下一次遇到就不会进queue

  • 133. Clone Graph

  • 133. Clone Graph

    https://leetcode-cn.com/problems/clone-graph/ (图片来源https:...

  • 133. Clone Graph

    给无向连通图中节点的引用,返回图的深拷贝。 DFS 对访问过的节点进行标记,然后用dfs 时间复杂度O(n),空间...

  • Leetcode 133. Clone Graph

    题意:克隆一个无向图。 思路:因为是克隆所以需要一个map来存储克隆的映射关系,我们可以从给定的第一个节点向它的n...

  • LeetCode 133. Clone Graph

    Clone an undirected graph. Each node in the graph contain...

  • 数据结构---图

    图的定义和术语 有向图(Directed graph) 无向图(Undirected graph) 图的边和弧的关...

  • DAG上的动态规划「二」

    有向无环图DAG算法中有时称有向无环图为DAG ( Directed Acyclic Graph)。所谓有向无环图...

网友评论

      本文标题:133. Clone Graph 复制无向图

      本文链接:https://www.haomeiwen.com/subject/tldqoftx.html