clone-graph
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                clone-graph
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                1. clone-graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
思路:dfs,其實就是遞歸。
1 /** 2 * Definition for undirected graph. 3 * struct UndirectedGraphNode { 4 * int label; 5 * vector<UndirectedGraphNode *> neighbors; 6 * UndirectedGraphNode(int x) : label(x) {}; 7 * }; 8 */ 9 class Solution { 10 public: 11 unordered_map<UndirectedGraphNode *,UndirectedGraphNode *> hash; 12 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { 13 if(!node)return node; 14 if(hash.find(node)==hash.end()){ 15 hash[node] = new UndirectedGraphNode(node->label); 16 for(auto x : node->neighbors){ 17 (hash[node]->neighbors).push_back(cloneGraph(x)); 18 } 19 } 20 return hash[node]; 21 } 22 };?
轉載于:https://www.cnblogs.com/xctcherry/p/8783482.html
總結
以上是生活随笔為你收集整理的clone-graph的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: lol肉有哪些英雄
 - 下一篇: 新综艺演员的诞生评价两极化,章子怡刘烨郑