职务地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/

题意:对一个有回路的链表的深复制

解题:这道题我AC了之后才发现理解错了题意,然后就參考了以下这篇文章。

假设想看这道题的解题思路的话,请移步http://www.2cto.com/kf/201310/253477.html

拓展:假如题目给的节点不是链表的头节点。而是链表中的随意一个节点,在保证从给的点能遍历所有节点的情况下,深复制这个链表。

那么该怎么做呢?

思路:事实上一想,和原题的区别仅仅有节点是否是头节点而已,那么我们就从给的这个节点找到原节点即可了。

遍历的过程用dfs,再用map来推断一个节点是否被遍历过。如今唯一的难点就是怎样找到头节点,这里用并查集就OK了。

路径压缩后并查集的复杂度接近O(1),因为map的复杂度接近O(logN),这样dfs的复杂度大概是O(NlogN)。

技巧:dfs到一个新的节点时,用map将其映射成一个唯一的整数cnt,并在这时初始化并查集fa[cnt]。

//#include "stdafx.h"
#include<iostream>
#include<string.h>
#include <map>
#include<set>
using namespace std;
int fa[10024];//节点数
inline int findfa(int x){
if(fa[x] == x) return x;
while(x!=fa[x]){
x=fa[x];
}
return x;
} inline int merge(int x,int y){
int fa1=findfa(x), fa2=findfa(y);
if(fa1!=fa2){
fa[fa2] = fa1;
}
return fa1;
} struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
}; class Solution {
map<RandomListNode*,int> mp;
map<RandomListNode*,int>::iterator it;
int cnt;
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if(!head) return head;
/***********凝视部分是拓展的解法************
init();
dfs(head,1);
int faNum = findfa(1);
for(it=mp.begin();it!=mp.end();++it){
if(it->second == faNum){
head = it->first;
break;
}
}
***********************************************/
return deepCopy(head);
}
void init(){
memset(fa,0,sizeof(fa));
cnt=0; }
void dfs(RandomListNode *ptr, int faCnt){
if(ptr == NULL) return;
it=mp.find(ptr);
if(it!=mp.end()) return; mp[ptr] = ++cnt;
fa[cnt] = cnt;//初始化
fa[cnt] = findfa(faCnt>0? faCnt:cnt);
RandomListNode *next = ptr->next;
RandomListNode *random = ptr->random;
it=mp.find(next);
if(it==mp.end()){
dfs(next,faCnt>0?faCnt:cnt);
}
else{
fa[mp[next]] = fa[cnt];
} it=mp.find(random);
if(it==mp.end()){
dfs(random,0);//0表示不能确定其父节点
}
} //非拓展部分解法
RandomListNode* deepCopy(RandomListNode *head){
//在原链表上双倍复制链表
RandomListNode *ptr1 = head, *ptr2 = NULL;
while(ptr1 != NULL){
ptr2 = new RandomListNode(ptr1->label);
ptr2->next = ptr1->next;
ptr1->next = ptr2;
ptr1=ptr2->next;
} //复制原链表的random关系
ptr1 = head, ptr2 = NULL;
while(ptr1 != NULL){
ptr2 = ptr1->next;
if(ptr1->random)
ptr2->random = ptr1->random->next;
ptr1=ptr2->next;
} //将原列表分裂成两个列表
RandomListNode *H =NULL, *ptr3;
ptr1 = head, ptr2 = NULL;
while(ptr1 != NULL){
ptr2 = ptr1->next;
if(H==NULL){
H = ptr2;
ptr3 = H;
}
else{
ptr3->next = ptr2;
ptr3 = ptr2;
}
ptr1->next = ptr2->next;
ptr1 = ptr1->next;
}
return H;
}
};

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[LeetCode OJ] Copy List with Random Pointer 扩大的更多相关文章

  1. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

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

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

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

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

  4. Java for LeetCode 138 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 (hard)

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

  6. leetcode 138. Copy List with Random Pointer ----- java

    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 【 Copy List with Random Pointer 】 python 实现

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

随机推荐

  1. Perl自动释放Licence启动Verdi

    Perl自动释放Licence启动Verdi 在工作中,遇到verdi的License不够的情况,某些人占用了多个License,为及时获得一个可用的License,写了一个perl来kill运行时间 ...

  2. Java算法--串的简单处理

    题目例如以下: 串的处理 在实际的开发工作中.对字符串的处理是最常见的编程任务. 本题目即是要求程序对用户输入的串进行处理.详细规则例如以下: 1. 把每个单词的首字母变为大写. 2. 把数字与字母之 ...

  3. c# 调用ArcEngine的GP工具

    转自原文c# 调用ArcEngine的GP工具,AE调用GP工具 IAoInitialize m_AoInitialize = new AoInitializeClass(); esriLicense ...

  4. ios开发事件处理之:三 :寻找最合适的view

    1:事件的产生与传递: 2:寻找最合适的view:如何查找最合适的view:需要三步:1:先判断自身是否能接受触摸事件 2:判断触摸点是否在自己身上 3:若前两条都满足,则其会从后往前遍历所有子控件( ...

  5. 开发文档生成工具----强大的Doxygen工具使用手册

    张三:假如我们自己开发了一个类库,怎么做一个方便阅读的文档呢? 李四:一个方法一个方法地写呗,就像写Excel文档一下. 张三:啊,你out了,这多慢呀.为什么不玩玩doxygen工具,它能帮你生成文 ...

  6. JS中的JSON对象 定义和取值

    1.JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任 ...

  7. [Most.js] Create Streams From Single Values With Most.js

    Most provides many means for creating streams, the simplest of which is the offunction. In this less ...

  8. Android应用程序文件缓存getCacheDir()和getExternalCacheDir()

    如果Android引用程序需要缓存临时文件,系统提供了一个可管理的“内部缓存”和一个不可管理的“外部缓存”,分别调用getCacheDir()和getExternalCacheDir()方法,可以从当 ...

  9. javaScript DOM编程经常使用的方法与属性

    DOM是Document Object Model文档对象模型的缩写.依据W3C DOM规范,DOM是一种与浏览器,平台,语言无关的接口,使得你能够訪问页面其它的标准组件. Node接口的特性和方法 ...

  10. iOS中打电话、打开网址、发邮件、发短信等

    常用小功能 小功能简介 iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信等 打电话-方法1 最简单最直接的方式:直接跳到拨号界面 NSURL *url = [ ...