zoukankan      html  css  js  c++  java
  • LeetCode "Clone Graph"

    A BFS usage.

    class Solution {
    public:
        UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
            if (!node) return NULL;
    
            UndirectedGraphNode *pRet = NULL;
    
            queue<UndirectedGraphNode*> q;
            unordered_map<int, UndirectedGraphNode *> map;
            
            q.push(node);        
            while (!q.empty())
            {
                UndirectedGraphNode *p = q.front();
                //    Create new node
                UndirectedGraphNode *pNew = NULL;
                if (map.find(p->label) == map.end())
                {
                    pNew = new UndirectedGraphNode(p->label);
                    map[p->label] = pNew;
                    if (!pRet)    pRet = pNew;
                }
                else
                {
                    pNew = map[p->label];
                }
    
                for (UndirectedGraphNode *pC : p->neighbors)
                {
                    UndirectedGraphNode *pNewC = NULL;
                    if (map.find(pC->label) == map.end())
                    {
                        pNewC = new UndirectedGraphNode(pC->label);
                        map[pC->label] = pNewC;
                        q.push(pC);
                    }
                    else
                    {
                        pNewC = map[pC->label];
                    }
                    pNew->neighbors.push_back(pNewC);                
                }
                q.pop();
            }
            return pRet;
        }
    };
  • 相关阅读:
    生成器,生成器表达式。
    device busy
    memcached
    ps f
    Eviews9.0---软件安装
    免费提取百度文库 doc 文件
    Matlab---length函数
    Matlab 路径函数
    matlab中disp函数的简单用法
    MATLAB---dir函数
  • 原文地址:https://www.cnblogs.com/tonix/p/3872804.html
Copyright © 2011-2022 走看看