[LeetCode OJ] Copy List with Random Pointer 扩大
职务地址: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 扩大的更多相关文章
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- [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 ...
- [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 ...
- 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 ...
- 【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 ...
- 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 ...
- LeetCode _ Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 【 Copy List with Random Pointer 】 python 实现
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- ios开发swift学习第三天:逻辑分支
一. 分支的介绍 分支即if/switch/三目运算符等判断语句 通过分支语句可以控制程序的执行流程 二. if分支语句 和OC中if语句有一定的区别 判断句可以不加() 在Swift的判断句中必须有 ...
- USACO--2.1The Castle
思路:这个题目难在建图,開始的时候我想把每一个房间没有墙的面求出来,然后再和他邻近的房间加上一条边进行建图,后面发现要通过题目给定的条件求出房间那个面没有墙是十分困难的:后面參考了别人的思路,我们记录 ...
- thinkphp中如何实现无限级分类?
thinkphp中如何实现无限级分类? 一.总结 1.数据表设计+递归算法 二.php实现无限级分类实例总结 1.数据库数据如下: 2.任务需求:给一个id,求自己和所有父亲. 3.实现代码如下:th ...
- 【codeforces 754A】Lesha and array splitting
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- ie7easyui的书写要规范
在书写easyui对象的属性,有时候习惯在,属性的末尾再加一个“,”,这个在高版本浏览器是没事的,但是在ie7及以下,会有报错
- Spring mvc redirect跳转路径问题
SpringMVC重定向视图RedirectView小分析 前言 SpringMVC是目前主流的Web MVC框架之一. 本文所讲的部分内容跟SpringMVC的视图机制有关,SpringMVC的视图 ...
- [javase学习笔记]-6.6 基本数据类型參数与引用数据类型參数的传递过程
这一节基本数据类型參数和引用数据类型參数的传递过程. 数据类型參数和引用參数我们在前面章节中都已涉及到了,那么我们来看看以下的两段代码: //基本数据类型參数传递 class Demo { publi ...
- js把其他类型转化成字符串
js把其他类型转化成字符串 一.总结 一句话总结:类型转换中的强制类型转换分为类型转换函数和类型名强制.js后一种和其它语言不同,是类型类的构造方法.String() 二.js把其他类型转化成字符串 ...
- UITableView的一些常用设置和代理方法
- (void)viewDidLoad { [super viewDidLoad]; tableview = [[UITableView alloc]initWithFrame:CGRectMake( ...
- scala读写文件 comparing values of types Unit and Int using `!=' will always yield true
由于scala没有对写入文件的支持,所以写文件时通常借助java进行IO操作 //方式一(小文件) /* val s1 = Source.fromFile("D:\\inputword\\h ...