/**
* 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. cocos2d-x v3.2环境配置(现在3.x版本号可以配置该)

     这里介绍的是Windows环境下的cocos2d-x配置. 前提: •Windows 7+ •cocos2d-x v3.2版本号(能够在http://www.cocos2d-x.org/down ...

  2. MKNetWorkKit打印URL

    -(void)initNewUrl:(NSString *)urlString param:(NSMutableDictionary *)_paramDic{ //拼接參数至URL NSMutable ...

  3. window批处理-4.call

    作用: 批处理中调用还有一个批处理或调用行号后的全部命令 格式: call [FileName] [:label] demo: call.bat: @echo off echo 開始调用called ...

  4. Data source rejected establishment of connection, message from server: &quot;Too many connections&quot;

    错误叙述性说明: 測试一段时间没有不论什么问题.今天突然用户无法登录,报错如Data source rejected establishment of connection,  message fro ...

  5. Android 在非主线程无法操作UI意识

    Android在应用显示Dialog是一个非常easy事儿,但我从来没有尝试过Service里面展示Dialog. 经验UI操作要在主线程,本地的服务Service是主线程里没错,可是远程servic ...

  6. fastjson经常用法

    首先,JSON究竟是什么? JSON就是一串字符串 仅仅只是元素会使用特定的符号标注. {} 双括号表示对象 [] 中括号表示数组 "" 双引號内是属性或值 : 冒号表示后者是前者 ...

  7. Azure VM Public IP设置

    Azure虚拟机的Public IP是用于客户端直连云中的虚拟机,可以认为是一个外网IP,一般我们为虚拟机设置终结点,例如HTTP的80端口,如果使用Public IP可以不使用Azure Porta ...

  8. Linux内核分析(三)----初识linux内存管理子系统

    原文:Linux内核分析(三)----初识linux内存管理子系统 Linux内核分析(三) 昨天我们对内核模块进行了简单的分析,今天为了让我们今后的分析没有太多障碍,我们今天先简单的分析一下linu ...

  9. Thinkpad E431 解决无线网卡无法开启

    Thinkpad E431无线网卡无法开启 现象再现: Thinkpad E431新机,原装win8系统,使用win7光盘换为win7系统,官方下载驱动程序,安装后无线上网正常. 点击功能软件Acce ...

  10. jquery中 $ 和 jQuery 及 $() 的差别

    用过jquery的人都知道,jquery有两种使用方法,一种是$,另一种是jQuery,那么这两种方式在使用上有什么差别呢? 答案是这两种使用方法没什么差别,仅仅是别名而已,用$要比jQuery简短一 ...