苏淳这个倒霉蛋!# Stock
h*g
1 楼
刚做了下 Graph Clone, Output Limit Exceeded. 查了半天也没看出来哪里有问题,
附上代码,请大牛门挑挑毛病,谢拉!
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if (!node) return NULL;
UndirectedGraphNode * nodecopy;
queue que;
unordered_map map;
nodecopy=new UndirectedGraphNode(node->label);
que.push(node);
map[node]=nodecopy;
while(!que.empty()){
UndirectedGraphNode* current=que.front();
que.pop();
int nsz=current->neighbors.size();
for(int i=0;i UndirectedGraphNode* neighbor=new UndirectedGraphNode(
current->neighbors[i]->label);
if(map.find(current->neighbors[i])==map.end()){
map[current]->neighbors.push_back(neighbor);
map[current->neighbors[i]]=neighbor;
que.push(current->neighbors[i]);
}
else{
map[current]->neighbors.push_back(neighbor);
}
}
}
return nodecopy;
}
附上代码,请大牛门挑挑毛病,谢拉!
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if (!node) return NULL;
UndirectedGraphNode * nodecopy;
queue
unordered_map
nodecopy=new UndirectedGraphNode(node->label);
que.push(node);
map[node]=nodecopy;
while(!que.empty()){
UndirectedGraphNode* current=que.front();
que.pop();
int nsz=current->neighbors.size();
for(int i=0;i
current->neighbors[i]->label);
if(map.find(current->neighbors[i])==map.end()){
map[current]->neighbors.push_back(neighbor);
map[current->neighbors[i]]=neighbor;
que.push(current->neighbors[i]);
}
else{
map[current]->neighbors.push_back(neighbor);
}
}
}
return nodecopy;
}