多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php框架 > 框架設(shè)計 > leetcode || 133、Clone Graph

leetcode || 133、Clone Graph

來源:程序員人生   發(fā)布時間:2015-05-21 08:10:33 閱讀次數(shù):3289次

problem:

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 / \_/

Hide Tags
 Depth-first Search Breadth-first Search Graph
題意:復(fù)制圖(結(jié)構(gòu)和數(shù)據(jù)不變,要新建節(jié)點)

thinking:

(1)要新建圖的各個節(jié)點,保持鄰接關(guān)系不變。

(2)采取unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> 存儲原節(jié)點和新節(jié)點。而不是unordered_map<int, UndirectedGraphNode*>,
          效力要高很多

(3)采取BFS思想,將原節(jié)點的鄰接節(jié)點全部入?;蚨褩?,遍歷節(jié)點。

(4)map中查找key是不是存在可以調(diào)用find(),也能夠調(diào)用count(),后者效力更高

(5)提交沒通過,結(jié)果不正確:

Input:{0,1,5#1,2,5#2,3#3,4,4#4,5,5#5}

Output:{0,5,1#1,5,2#2,3#3,4,4#4,5,5#5}

Expected:{0,1,5#1,2,5#2,3#3,4,4#4,5,5#5}

其實,結(jié)果是正確的,由于對無向圖,節(jié)點出現(xiàn)的順序不影響圖的結(jié)構(gòu),只能說這個驗證程序只驗證了1種結(jié)果

code:

class Solution { public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> record; if(node == NULL) return node; stack<UndirectedGraphNode*> queue; queue.push(node); while(!queue.empty()) { UndirectedGraphNode *nextNode = queue.top(); queue.pop(); if(!record.count(nextNode)) { UndirectedGraphNode *newNode = new UndirectedGraphNode(nextNode->label); record[nextNode] = newNode; } for(int i = nextNode->neighbors.size()⑴; i >= 0 ; i --) { UndirectedGraphNode *childNode = nextNode->neighbors[i]; if(!record.count(childNode)) { UndirectedGraphNode *newNode = new UndirectedGraphNode(childNode->label); record[childNode] = newNode; queue.push(childNode); } record[nextNode]->neighbors.push_back(record[childNode]); } } return record[node]; } };


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 欧美18一19sex性瑜伽hd | 久久国产免费福利资源网站 | 一区二区三区 | www.亚洲天堂网 | 亚洲日本视频在线 | 国产在线不卡免费播放 | 国产日产欧产麻豆精品精品推荐 | 欧美free嫩交videoxxx | 欧美日性 | 国产亚洲精品自在久久不卡 | 午夜视频在线观看免费视频 | 欧美不卡视频 | 久久久久久久久久久久久久久久久久久久 | 三级做爰大爽长视频在线观看 | 国内精品久久久久影院嫩草 | 亚洲天堂麻豆 | 印度最猛性xxxxx | www.色亚洲| 在线欧美一级毛片免费观看 | 五月天校园春色 | 亚洲天堂手机版 | 亚洲天堂在线视频观看 | 日本高清中文字幕一区二区三区 | 亚洲xxx视频 | 最近中文字幕mv手机免费高清 | 欧美大片aaaa一级毛片 | 中文字幕天天躁夜夜狠狠综合 | 日本一区二区三区精品国产 | 亚洲成年人在线观看 | 欧洲美女a视频一级毛片 | 三级黄在线 | 国产v综合v亚洲欧美 | 亚洲精品久久久久久久网站 | 一区二区三区久久精品 | 国产成人乱码一区二区三区在线 | 性做久久久久免费观看 | 欧美一级欧美一级毛片 | 波多野结衣在线播放视频 | 亚洲欧美校园 | 精品久久久99大香线蕉 | 亚洲 中文 欧美 日韩 在线人 |