lurenaa的博客

🥶map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
Node* copyRandomList(Node* head) {
Node* new_prev = new Node(0);
new_prev->next = helper(head);
return new_prev->next;
}
Node* helper(Node* nd) {
if(!nd)
return nullptr;
if(mp.count(nd))
return mp[nd];
Node* neo = new Node(nd->val);
mp[nd] = neo;
neo->next = helper(nd->next);
neo->random = helper(nd->random);
return neo;
}
private:
map<Node*, Node*> mp;
};

Accepted

18/18 cases passed (20 ms)

Your runtime beats 44.06 % of cpp submissions

Your memory usage beats 69.77 % of cpp submissions (15 MB)