由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 求帮忙看看这个clone graph的解法。弄半天还是不对。 多谢!
相关主题
Clone graph贡献Amazon的电面经验
LeetCode 新题 graph clone面试题:Data structure to find top 10 search strings
一道fb的题,clone a graph我恨iPhone@Facebook电面
菜鸟 贴一个 leetcode LRU Cache -- java代码,并求解疑惑。remove a node in O(1) from link list
An Old Question -- Top N Occurance Frequancelinklist exercise
leetcode 129【我自己写的LinkedList为什么总有错?】
word ladder ii 谁给个大oj不超时的?问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5
分别用LinkedList和HashMap构建字典树(Compact Trie)怎么做amazon onsite 面经
相关话题的讨论汇总
话题: node话题: graphcopy话题: mp话题: dfs
进入JobHunting版参与讨论
1 (共1页)
t**r
发帖数: 3428
1
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node) return NULL;
map mp;
return DFS(node, mp);
}
UndirectedGraphNode* DFS(UndirectedGraphNode* node, map<
UndirectedGraphNode*, UndirectedGraphNode*>& mp){
if(mp.find(node) != mp.end()) {
return mp[node];
}
UndirectedGraphNode* graphCopy = new UndirectedGraphNode(node->label
);
mp[node] = graphCopy;
for(auto neighbor: node->neighbors){
graphCopy->neighbors.push_back(DFS(node,mp));
}
return graphCopy;
}

};
Submission Result: Wrong Answer
Input: {-1,1#1}
Output: {-1,-1}
Expected: {-1,1#1}
d****n
发帖数: 233
2

for(auto neighbor: node->neighbors){
graphCopy->neighbors.push_back(DFS(node,mp));
}
改成
for(auto neighbor: node->neighbors){
graphCopy->neighbors.push_back(DFS(neighbor,mp));
}
另外,可以参考我的blog for the two ways to solve this problem:
http://codeanytime.blogspot.com/2014/11/clone-graph.html

【在 t**r 的大作中提到】
: class Solution {
: public:
: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
: if(!node) return NULL;
: map mp;
: return DFS(node, mp);
: }
: UndirectedGraphNode* DFS(UndirectedGraphNode* node, map<
: UndirectedGraphNode*, UndirectedGraphNode*>& mp){
: if(mp.find(node) != mp.end()) {

h**d
发帖数: 630
3
graphCopy->neighbors.push_back(DFS(node,mp));
这句node 改 neighbor
C****e
发帖数: 27
4
我今天也正好在做这题,看的是网络答案,但是不知道main函数要怎么写才能实现调用
输出结果呢?
public class clonegraph {
public static void main(String[] args){

}
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node==null)
return null;
LinkedList queue = new LinkedList<
UndirectedGraphNode>();
HashMap map = new HashMap<
UndirectedGraphNode, UndirectedGraphNode>();
UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
map.put(node,copy);
queue.offer(node);
while(!queue.isEmpty())
{
UndirectedGraphNode cur = queue.poll();
for(int i=0;i {
if(!map.containsKey(cur.neighbors.get(i)))
{
copy = new UndirectedGraphNode(cur.neighbors.get(i).label);
map.put(cur.neighbors.get(i),copy);
queue.offer(cur.neighbors.get(i));
}
map.get(cur).neighbors.add(map.get(cur.neighbors.get(i)));
}
}
return map.get(node);
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
amazon onsite 面经An Old Question -- Top N Occurance Frequance
问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5,leetcode 129
判断linkedlist是否palindrome最优解法是什么?word ladder ii 谁给个大oj不超时的?
求个java版本的binary tree serialization和deserialization分别用LinkedList和HashMap构建字典树(Compact Trie)怎么做
Clone graph贡献Amazon的电面经验
LeetCode 新题 graph clone面试题:Data structure to find top 10 search strings
一道fb的题,clone a graph我恨iPhone@Facebook电面
菜鸟 贴一个 leetcode LRU Cache -- java代码,并求解疑惑。remove a node in O(1) from link list
相关话题的讨论汇总
话题: node话题: graphcopy话题: mp话题: dfs