e***n 发帖数: 68 | 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.
*/ |
l********5 发帖数: 230 | 2 应该还是要考虑各种case。。。palantir很喜欢细节。。很想testing的题。。。 |
a********x 发帖数: 1502 | 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. : */
|
K*********n 发帖数: 2852 | 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. : */
|
e***n 发帖数: 68 | 5 不知道阿。。。反正面我的老美开着免提也
【在 K*********n 的大作中提到】 : 牛,这公司理你了啊,他们据说挺拽啊
|
q****m 发帖数: 177 | 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. : */
|
p*****2 发帖数: 21240 | |
l***i 发帖数: 1309 | 8 随便写了一个,大牛指点
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;
} |
d*********g 发帖数: 154 | 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; : }
|
l***i 发帖数: 1309 | 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; i
ans->things[i] = deepCopy(ptr->things[i]);
return ans;
} |
l***i 发帖数: 1309 | 11 嗯,刚刚看了leetcode的答案贴,思路差不多 |