Careercup - Facebook面试题 - 5412018236424192
2014-05-01 01:32
原题:
Given a linked list where apart from the next pointer, every node also has a pointer named random which can point to any other node in the linked list. Make a copy of the linked list.
题目:给定一个单链表,每个链表除了next指针以外,还有一个random指针,随机地指向nullptr或者链表中的某个节点。请设计算法完成一份链表的硬拷贝。
解法:Leetcode上有相关题目,请看题解LeetCode - Copy List with Random Pointer。
代码:
// http://www.careercup.com/question?id=5412018236424192
#include <unordered_map>
using namespace std; struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {};
}; class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int n; if(nullptr == head){
return nullptr;
} n = ;
RandomListNode *ptr; ptr = head;
while(ptr != nullptr){
++n;
mri[ptr] = n;
ptr = ptr->next;
} RandomListNode *root, *tail;
ptr = head; int i = ;
tail = root = nullptr;
while(ptr != nullptr){
++i;
if (root == nullptr) {
root = tail = new RandomListNode(ptr->label);
} else {
tail->next = new RandomListNode(ptr->label);
tail = tail->next;
}
mir[i] = tail;
ptr = ptr->next;
} RandomListNode *p1, *p2; p1 = head;
p2 = root;
while(p1 != nullptr){
if(p1->random != nullptr){
p2->random = mir[mri[p1->random]];
}
p1 = p1->next;
p2 = p2->next;
} mir.clear();
mri.clear(); return root;
}
private:
unordered_map<RandomListNode *, int> mri;
unordered_map<int, RandomListNode *> mir;
};
Careercup - Facebook面试题 - 5412018236424192的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- Bootstrap基础学习-1
Bootstrap是一个基于栅格结构的前端结构框架(当然也有JS,JQuery),它的优点是内容框架能够迅速搭建起来,基于媒介查询可以使搭建的页面迅速的适应不同的用户端,无论是手机,平板,还是PC,基 ...
- 瀑布流布局--原生JavaScript
HTML(注意包裹关系,方便js调用) <body> <div id="main"> <div class="box"> & ...
- C#垃圾回收机制
C#属于托管的面相对象的语言,内存回收机制就是一个代表, C#有一套类似"全自动"的垃圾回收机制,也就是虚拟机会自动来判断执行内存的回收, 我们一般常用的Dispose(),Usi ...
- Part 30 to 31 Talking about Interfaces in c#
Part 30 Interfaces in c# We create interfaces using interface keyword. just like classes interfaces ...
- 解决ashx文件下的Session“未将对象引用设置到对象的实例”
using System; using System.Collections.Generic; using System.Linq; using System.Web; using PPT_DAL; ...
- .net上传图片并转成二进制流
话不多说,直接上代码 <input id="InputFile" style="width: 399px" type="file" r ...
- Python深拷贝和浅拷贝
1- Python引用计数[1] 1.1 引用计数机制 引用计数是计算机编程语言中的一种内存管理技术,是指将资源(可以是对象.内存或磁盘空间等等)的被引用次数保存起来,当被引用次数变为零时就将其释放的 ...
- 如何更改Json.NET的序列化规则
我想要使序列化出来的JSON都是小写,可以通过建立 LowercaseContractResolver:DefaultContractResolver 来实现, 创建Custom ContractRe ...
- Apache+lvs高可用+keepalive(主从+双主模型)
Apache+lvs高可用+keepalive(主从+双主模型) keepalive实验准备环境: httpd-2.2.15-39.el6.centos.x86_64 keepalived-1 ...
- Matlab2012a第一次安装打不开 查找程序安装类时出错
打开bin文件夹下的matlab!!!!!!进行激活~