/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {//为了可以高速定位某个节点,採用确定性映射的方式,将复制链表的节点作为原链表相应节点的下一个节点
public:
RandomListNode *copyRandomList(RandomListNode *head) {
//每一个节点指向其复制链表相应的节点,从而可以高速定位该节点
if(!head)return NULL;
RandomListNode *p,*q;
p=head;
while(p){
q=new RandomListNode(p->label);
q->next=p->next;
p->next=q;
p=q->next;
}
p=head;
while(p){
q=p->next;
if(p->random)
q->random=p->random->next;
p=q->next;
}
p=head;
RandomListNode*head2=p->next;
q=head2;
while(p){
p->next=q->next;
p=p->next;
if(p){
q->next=p->next;
q=q->next;
}
}
return head2;
}
};

Clone Graph

类似的,对于图的复制,必须找到一种能够对新图中节点进行映射,能高速定位新节点的地址,从而使新节点指向新节点。这里採用map映射。考虑到图节点的label可能反复(本题不反复),而节点地址不反复,所以以新旧节点为键值对。

/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
map<UndirectedGraphNode*,UndirectedGraphNode*>mp;
map<UndirectedGraphNode*,UndirectedGraphNode*>::iterator bg;
UndirectedGraphNode* dfs(UndirectedGraphNode*p){
if(!p)return NULL;
if((bg=mp.find(p))!=mp.end())
return bg->second;
UndirectedGraphNode *q;
mp[p]=q=new UndirectedGraphNode(p->label);
for(int i=0,m=p->neighbors.size();i<m;++i){
q->neighbors.push_back(dfs(p->neighbors[i]));
}
return q;
}
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node)return NULL;
dfs(node);
return mp[node];
}
};

[LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制的更多相关文章

  1. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  3. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

  4. LeetCode——Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

  6. Leetcode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  7. [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. LeetCode – Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. 【LeetCode】138. Copy List with Random Pointer 复制带随机指针的链表 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号:负雪明烛 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https:/ ...

随机推荐

  1. 如果不能显示真正的考验个别车型toast问题解决

    当真正的考验个别车型toast不显示信息,找到两个解决方式.不知还有什么其他有效方法.期待大神们的分享!.! 1.手动方案:设置-->应用软件管理-->相应APP-->勾选显示通知框 ...

  2. 给AspNetPager分页控件添加bootstrap样式

    AspNetPager分页控件算是比较好用的一个分页控件了.想要结合bootstrap使用,官方代码入口 .pagination a[disabled]{ color: #777;cursor: no ...

  3. Vijos.1096 津津储蓄计划

    见问题: https://vijos.org/p/1096 主题概述 津津的零花钱一直都是自己的管理.每月初的母亲津津300美元,津津将于本月支出预算.而且总是做同样的实际支出与预算.  为了让津津学 ...

  4. RH253读书笔记(7)-Lab 7 Electronic Mail

    Lab 7 Electronic Mail Goal: To build common skills with MTA configuration Estimated Duration: 90 min ...

  5. 使用JS意识到自己主动提交表单

    今天将需要chat集成到客户的网站上去,注册用户链接登录这个网站后点击实现网站直接登录chat向上.我不停chat原来的登录界面,采纳JS当页面跳转技术,随着时间的推移自己主动填写表格.自己主动提交表 ...

  6. C# 获得Excel工作簿Sheet页面(工作表)集合的名称

    #region 获取Excel工作薄中Sheet页(工作表)名集合 /// <summary> /// 获取Excel工作薄中Sheet页(工作表)名集合 /// </summary ...

  7. JavaScript中五种常见运算符

    一. in运算符 in运算符希望它的左操作数是一个字符串或可以转换为字符串,希望它的右操作数是一个对象.如果右侧的对象拥有一个名为左操作数值的属性名,那么表达式返回true.例如: var point ...

  8. javascript/jquery读取和修改HTTP headers

    javascript/jquery读取和修改HTTP headers jquery修改HTTP headers jQuery Ajax可以通过headers或beforeSend修改request的H ...

  9. 未能加载文件或程序集 Newtonsoft.Json, Version=4.5.0.0 的报错,解决方法

    使用httpclient测试webapi的时候客户端报错: {"未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, P ...

  10. HDU 3415 Max Sum of Max-K-sub-sequence 最长K子段和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=3415 意甲冠军:环.要找出当中9长度小于等于K的和最大的子段. 思路:不能採用最暴力的枚举.题目的数据量是 ...