avatar
e*n
1
struct Thing {
int length;
struct Thing *things[];
};
/* Return a deep copy of thing. The object graph formed by the copy
* should have the same structure as the object graph of the original,
* but they should share no references.
*/
avatar
l*5
2
应该还是要考虑各种case。。。palantir很喜欢细节。。很想testing的题。。。
avatar
a*x
3
http://www.leetcode.com/2012/05/clone-graph-part-i.html
very similar, I think

【在 e***n 的大作中提到】
: struct Thing {
: int length;
: struct Thing *things[];
: };
: /* Return a deep copy of thing. The object graph formed by the copy
: * should have the same structure as the object graph of the original,
: * but they should share no references.
: */

avatar
K*n
4
牛,这公司理你了啊,他们据说挺拽啊

【在 e***n 的大作中提到】
: struct Thing {
: int length;
: struct Thing *things[];
: };
: /* Return a deep copy of thing. The object graph formed by the copy
: * should have the same structure as the object graph of the original,
: * but they should share no references.
: */

avatar
e*n
5
不知道阿。。。反正面我的老美开着免提也

【在 K*********n 的大作中提到】
: 牛,这公司理你了啊,他们据说挺拽啊
avatar
q*m
6
先简化一下 比如如何 deep copy
struct Thing{
int length,
struct Thing *next;
}
吧,这个 deep copy就相当于重新 create一个 linked list

【在 e***n 的大作中提到】
: struct Thing {
: int length;
: struct Thing *things[];
: };
: /* Return a deep copy of thing. The object graph formed by the copy
: * should have the same structure as the object graph of the original,
: * but they should share no references.
: */

avatar
p*2
7
DFS就可以了吧。
avatar
l*i
8
随便写了一个,大牛指点
Thing* deepCopy(Thing *ptr)
{
if (ptr == NULL) return NULL;
Thing *ans = new Thing;
ans->length = ptr->length;
for (int i=0; ians->things[i] = deepCopy(ptr->things[i]);
return ans;
}
avatar
d*g
9

貌似遇到circle就无限循环了

【在 l***i 的大作中提到】
: 随便写了一个,大牛指点
: Thing* deepCopy(Thing *ptr)
: {
: if (ptr == NULL) return NULL;
: Thing *ans = new Thing;
: ans->length = ptr->length;
: for (int i=0; i: ans->things[i] = deepCopy(ptr->things[i]);
: return ans;
: }

avatar
l*i
10
有道理。再想想。搞个map记录已经见过的node
Thing* deepCopy(Thing *ptr, map &mp)
{
if (ptr == NULL) return NULL;
if (mp.find(ptr) != mp.end()) return mp[ptr];
Thing *ans = new Thing; mp[ptr] = ans;
ans->length = ptr->length;
for (int i=0; ians->things[i] = deepCopy(ptr->things[i]);
return ans;
}
avatar
l*i
11
嗯,刚刚看了leetcode的答案贴,思路差不多
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。