职务地址: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. 应用 Valgrind 发现 Linux 程序的内存问题及交叉编译for arm

    Valgrind 概述 体系结构 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个框 ...

  2. runtimeException也是能够捕获的

    如题, 运行结果: bbb abcdef @Test public void testRuntimeException() { ; try { aaa333(); } catch (Exception ...

  3. Android JNI编程(五)——C语言的静态内存分配、动态内存分配、动态创建数组

    版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 一:什么是静态内存什么又是动态内存呢? 静态内存:是指在程序开始运行时由编译 ...

  4. 百度富文本编辑器ueditor使用启示

    百度富文本编辑器ueditor使用启示 一.总结 一句话总结:使用工具,多去看官方demo,非常详细. 二.百度富文本编辑器ueditor使用启示 官方完整demo 官方完整demo对应的源代码 &l ...

  5. Deepin系统更新apt-get源

    1.复制原文件备份sudo cp /etc/apt/source.list /etc/apt/source.list.bak2.编辑源列表文件sudo vim /etc/apt/source.list ...

  6. mui常用功能链接地址

    1.下拉刷新mui.pullToRefresh插件http://ask.dcloud.net.cn/article/12152.打包app权限列表http://ask.dcloud.net.cn/ar ...

  7. CSS学习小结

    接触了B/S的东西之后才发现自己须要学习的东西太多了.html.xml.JavaScript.jquery.HTMLdom.VBScript.ajax.jquery.json等等技术都是须要我们一一研 ...

  8. HTML中DOM对象的属性和方法的层级关系是怎样的?(目录即层次)

    HTML中DOM对象的属性和方法的层级关系是怎样的?(目录即层次) 一.总结 一句话总结:目录就是测试题 1.document取得元素(get element)的方式有哪几种? 解答:四种,分别是id ...

  9. 学习鸟哥的Linux私房菜笔记(8)——文件查找与文件管理2

    四.压缩 gzip, gunzip Linux标准压缩工具 对文本文件可以达到75%的压缩率 compress, uncompress 旧的Unix压缩工具 bzip2, bunzip2 更新的Lin ...

  10. CF337D Book of Evil - 树型dp

    传送门 题目大意: 一棵树上有一个特殊点,特殊点可以影响距离小于等于d的点,现在告诉被影响的点,问特殊点可以在几个点上. 题目分析: 对题意进行转化:求到被影响点的最大距离小于等于d的点数目. 然后就 ...