查看: 2287| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] Clone Graph 问题出错(Output Limit Exceeded)

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
用dfs写的,debug了半天,找不出问题,找版上大牛求助!
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;

  4. struct UndirectedGraphNode {
  5.         int label;
  6.         vector<UndirectedGraphNode *> neighbors;
  7.         UndirectedGraphNode(int x) : label(x) {};
  8. };


  9. class Solution {
  10. public:
  11.         UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
  12.                 if (node == NULL) return NULL;
  13.                 UndirectedGraphNode *head = new UndirectedGraphNode(node->label);
  14.                 dfs(node, head);
  15.                 return head;
  16.         }
  17.         void dfs(UndirectedGraphNode *node, UndirectedGraphNode *copy) {
  18.                 if ( !is_visited(node) ) {
  19.                         visited.push_back(node->label);       
  20.                         //cout << node->label << endl;
  21.                         vector<UndirectedGraphNode *> vec = node->neighbors;
  22.                         for (int i=0; i<vec.size(); i++) {
  23.                                 UndirectedGraphNode *n = new UndirectedGraphNode(vec[i]->label);
  24.                                 copy->neighbors.push_back(n);
  25.                                 //cout << n->label << ": " << vec[i]->label << endl;  
  26.                                 dfs(vec[i], n);
  27.                         }
  28.                 }       
  29.         }
  30.         bool is_visited(UndirectedGraphNode *node) {
  31.                 for (int i=0; i<visited.size(); i++) {
  32.                         if (visited[i] == node->label) return true;
  33.                 }
  34.                 return false;
  35.         }       
  36. private:
  37.         vector<int> visited;
  38. };

  39.         vector<int> visited;
  40.         bool is_visited(UndirectedGraphNode *node) {
  41.                 for (int i=0; i<visited.size(); i++) {
  42.                         if (visited[i] == node->label) return true;
  43.                 }
  44.                 return false;
  45.         }
  46.         void dfs(UndirectedGraphNode *node) {
  47.                 if ( !is_visited(node) ) {
  48.                         visited.push_back(node->label);       
  49.                         //cout << node->label << endl;
  50.                         vector<UndirectedGraphNode *> vec = node->neighbors;
  51.                         for (int i=0; i<vec.size(); i++) {
  52.                                 cout << node->label << "->neighbors: " << vec[i]->label << endl;  
  53.                                 dfs(vec[i]);
  54.                         }
  55.                 }       
  56.         }

  57.        

  58. int main()
  59. {
  60.         UndirectedGraphNode *node0 = new UndirectedGraphNode(0);
  61.         UndirectedGraphNode *node1 = new UndirectedGraphNode(1);
  62.         UndirectedGraphNode *node2 = new UndirectedGraphNode(2);
  63.        
  64.         node0->neighbors.push_back(node1);
  65.         node0->neighbors.push_back(node2);
  66.         node0->neighbors.push_back(node0);
  67.         node0->neighbors.push_back(node0);

  68.         node1->neighbors.push_back(node0);
  69.         node1->neighbors.push_back(node2);

  70.         node2->neighbors.push_back(node0);
  71.         node2->neighbors.push_back(node1);

  72.         Solution s;
  73.         UndirectedGraphNode *n = s.cloneGraph(node0);
  74.         //cout << n->neighbors[0]->label << endl;
  75.         //cout << n->neighbors[1]->label << endl;
  76.         //cout << n->neighbors.size() << endl;
  77.         dfs(n);
  78. }
复制代码

上一篇:[请教]LeetCode Merge Intervals的问题
下一篇:[请教]leetcode的valid number 一题
🔗
 楼主| fangl086 2014-8-11 19:43:17 | 只看该作者
全局:
  1. class Solution {
  2. public:
  3.         UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
  4.                 if (node == NULL) return node;
  5.                 unordered_map<int, UndirectedGraphNode *> visited;
  6.                 return dfs(node, visited);
  7.         }
  8.         UndirectedGraphNode *dfs(UndirectedGraphNode *node, unordered_map<int,
  9.                                                                 UndirectedGraphNode *> &visited) {
  10.                 UndirectedGraphNode *copy = new UndirectedGraphNode(node->label);
  11.                 if (visited.find(node->label) != visited.end())
  12.                         return visited[node->label];
  13.                        
  14.                 visited[node->label] = copy;       
  15.                 vector<UndirectedGraphNode *> vec = node->neighbors;
  16.                 for (int i=0; i<vec.size(); i++) {
  17.                         copy->neighbors.push_back(dfs(vec[i], visited));
  18.                 }
  19.                 return copy;
  20.         }
  21. };
复制代码
找不出问题,放上AC的代码吧
奇怪的是将 return visited[node->label];  改为 return copy;
错误提示:Output Limit Exceeded

很奇怪的问题,先放在这里了。
回复

使用道具 举报

🔗
 楼主| fangl086 2014-8-14 12:18:23 | 只看该作者
全局:
我在想是不是可能内存超出了预期
有知道的,回复下,不要沉了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表