/**
* 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. Ant—Ant标签解释

            采用ant命令必须写ant命令脚本,脚本由非常多Ant标签组成.现在总结一下我也遇到过Ant标签: 版权声明:本文博主原创文章,博客,未经同意不得转载.

  2. Redis被攻击

    记一次Redis被攻击的事件   最近几个月非常忙,所以很少有时间写博客,这几天终于闲了一些,于是就在整理平时的一些笔记.恰好这几天Redis服务器发生了问题,就记录一下. 我司有两款分别是2B和2C ...

  3. [DEEP LEARNING An MIT Press book in preparation]Linear algebra

    线性代数是数学的一个重要分支,它经常被施加到project问题,要了解学习和工作深入研究的深度,因此,对于线性代数的深刻理解是非常重要的.下面是我总结的距离DL book性代数中抽取出来的比較有意思的 ...

  4. QlikView一年计算,以最新的销售数据

    总销量的新财年后年初今天是非常需要的学生经常会遇到,有两种思路: 1. 能Load当数据是生成一个称为场YTDFlag.这是本财年的时刻,本场会1,除此以外,0.因此,在报告中可使用非常方便Sum(S ...

  5. Grant的时候报错的解决:Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)

    mysql> grant all on *.* to 'root'@'192.168.1.1' identified by 'password'; ERROR 1045 (28000): Acc ...

  6. C#-默认显示前列-ShinePans

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. Maven直接部署Web应用Tomcat

    1. 下载解压版tomcat,并配置环境变量.所以tomcat你可以成功启动. 使用版本解压tomcat可以方便查看tomcat的后台输出的出错信息,便于调试. 2. 给tomcat配置用户名密码. ...

  8. POJ 2774 后缀数组:查找最长公共子

    思考:其实很easy.就在两个串在一起.通过一个特殊字符,中间分隔,然后找到后缀数组的最长的公共前缀.然后在两个不同的串,最长是最长的公共子串. 注意的是:用第一个字符串来推断是不是在同一个字符中,刚 ...

  9. (大数据工程师学习路径)第四步 SQL基础课程----其他(基础练习到此为止)

    一.准备 在正式开始本内容之前,需要先从github下载相关代码,搭建好一个名为mysql_shiyan的数据库(有三张表:department,employee,project),并向其中插入数据. ...

  10. 分享个人Vim型材

    大力支持开源精神.保持开源大旗,今天,我将分享我自己以及结合自己的实际使用互联网的vimrc,我可以给你下的参考,不要见笑哈,说明我rc我写了一个非常详细,可以看看详细.同时,我们也希望借此机会结识了 ...