LeetCode 133 Clone Graph (BFS || DFS)

23 篇文章 0 订阅
1 篇文章 0 订阅

大体题意:

这个题目看了许久 才发现就是一道水题,他就是给你一个无向图,建图的方式都不用你管,他是用vector  保存的!

在给你一个新的点,克隆出这个图来!第一次做leetcode,搞了半天。。

思路:

BFS或者DFS都行 只要能遍历一遍所有的点就好了!

这个图有自环, 加个vis判断是否重复访问即可!

因为是克隆图嘛,可以建立一个unordered_map <UndirectedGraphNode*,UndirectedGraphNode*> mp;

来一个原点和新点的映射!

详细见代码:

BFS版:

class Solution {
public:
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if (node == NULL)return NULL;
        unordered_map <UndirectedGraphNode*,UndirectedGraphNode*> mp;
        unordered_map <UndirectedGraphNode*,bool> vis;
        queue<UndirectedGraphNode*>q;
        mp.clear();
        vis.clear();
        while(!q.empty()) q.pop();
        q.push(node);
        vis[node] = false;
        while(!q.empty()){
            UndirectedGraphNode* u = q.front(); q.pop();
            if (vis[u])continue;
            vis[u] = true;
            if (!mp.count(u)) mp[u] = new UndirectedGraphNode (u->label);
            for (int i = 0; i < u->neighbors.size(); ++i){
                UndirectedGraphNode* v = u->neighbors[i];
                UndirectedGraphNode * tmp;
                if (!mp.count(v)){
                    tmp = new UndirectedGraphNode (v->label);
                    mp[v] = tmp;
                }else tmp = mp[v];

                q.push(v);
                mp[u]->neighbors.push_back(tmp);
            }
        }
        return mp[node];
    }
};

DFS版:

class Solution {

public:
    unordered_map<UndirectedGraphNode* ,UndirectedGraphNode* >mp;
    unordered_map<UndirectedGraphNode* ,bool >vis;
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if (node == NULL)return NULL;
        mp.clear();
        vis.clear();
        dfs(node);
        return mp[node];
    }
    void dfs(UndirectedGraphNode *node){
        vis[node] = true;
        if (!mp.count(node)) mp[node] = new UndirectedGraphNode(node->label);
        UndirectedGraphNode* u = mp[node];
        for (int i = 0; i < node->neighbors.size(); ++i){
            UndirectedGraphNode *v = node->neighbors[i];
            if (vis[v]) continue;
            if (!mp.count(v)) mp[v] = new UndirectedGraphNode (v->label);
            dfs(v);
        }
        for (int i = 0; i < node->neighbors.size(); ++i){
            UndirectedGraphNode *v = node->neighbors[i];
            u->neighbors.push_back(mp[v]);
        }
    }
};


133. Clone Graph

 
  My Submissions
  • Total Accepted: 85949
  • Total Submissions: 344554
  • Difficulty: Medium
  • Contributors: Admin

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.


OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use  # as a separator for each node, and  , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
      / \
     /   \
    0 --- 2
         / \
         \_/

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems
Have you met this question in a real interview? 
Yes
 
No

Discuss Pick One


C++   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public :
UndirectedGraphNode * cloneGraph ( UndirectedGraphNode * node ) {
}
} ;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值