Careercup - Microsoft面试题 - 5917873302142976
2014-05-12 06:56
原题:
A link list contains following elements
struct node{
int data;
node* next;
node* random;
}
Given head of such a linked list write a function who copies such a linked list and returns the head of the new list. So if in the original list first node points to fourth node in random the copy should have the same relation. The random pointer can point to any node including itself and more than two nodes can have random pointer to the same node.
Required time complexity O(n) and no extra space can be used (apart from the newly allocated memory which you will need to create the new list)
题目:Leetcode上有这题,Copy List with Random Pointer。不过要求做到O(n)时间以及O(1)空间。
解法:之前我用了哈希表,能够实现O(n)时间,但不能做到O(1)空间。查阅了别人的解法后我发现自己的思路果然还不够灵活。这种解法很巧妙,先将新节点按照“旧->新->旧->新->旧->新”穿插到旧链表中,然后执行一条关键语句:ptr->next->random = ptr->random->next。执行完了以后将两条链表拆开即可(归并链表的逆过程)。哎,又是一个需要灵感才能想出的算法。
代码:
// http://www.careercup.com/question?id=5917873302142976
#include <iostream>
#include <unordered_map>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode*random;
ListNode(int _val = ): val(_val), next(nullptr), random(nullptr) {};
}; class Solution {
public:
ListNode *copyListWithRandomPointer(ListNode *head) {
if (head == nullptr) {
return nullptr;
}
ListNode *new_head;
ListNode *p1, *p2; p1 = head;
while (p1 != nullptr) {
p2 = new ListNode(p1->val);
p2->next = p1->next;
p1->next = p2;
p1 = p1->next->next;
} p1 = head;
while (p1 != nullptr) {
p1->next->random = p1->random == nullptr ? nullptr : p1->random->next;
p1 = p1->next->next;
}
new_head = splitList(head); return new_head;
};
private:
ListNode *splitList(ListNode *head) {
ListNode *head1, *head2;
ListNode *ptr1, *ptr2; // head1 is the original list.
head1 = ptr1 = nullptr;
// head2 is the new list.
head2 = ptr2 = nullptr;
while (true) {
if (head == nullptr) {
break;
}
if (head1 == nullptr) {
head1 = ptr1 = head;
} else {
ptr1->next = head;
ptr1 = ptr1->next;
}
head = head->next; if (head == nullptr) {
break;
}
if (head2 == nullptr) {
head2 = ptr2 = head;
} else {
ptr2->next = head;
ptr2 = ptr2->next;
}
head = head->next;
} return head2;
};
}; void deleteList(ListNode *&head)
{
ListNode *ptr; ptr = head;
while (head != ptr) {
ptr = head;
head = head->next;
delete ptr;
}
} int main()
{
int val;
int n;
int i;
ListNode *head1, *head2;
ListNode *ptr;
unordered_map<int, ListNode *> um;
Solution sol; while (cin >> n && n > ) {
head1 = head2 = nullptr;
for (i = ; i < n; ++i) {
cin >> val;
if (head1 == nullptr) {
head1 = ptr = new ListNode(val);
} else {
ptr->next = new ListNode(val);
ptr = ptr->next;
}
um[i] = ptr;
} ptr = head1;
for (i = ; i < n; ++i) {
cin >> val;
if (val >= ) {
ptr->random = um[val];
} else {
ptr->random = nullptr;
}
ptr = ptr->next;
} head2 = sol.copyListWithRandomPointer(head1);
ptr = head2;
while (ptr != nullptr) {
cout << ptr->val << ' ';
cout << (ptr->random != nullptr ? ptr->random->val : -) << endl;
ptr = ptr->next;
} deleteList(head1);
deleteList(head2);
} return ;
}
Careercup - Microsoft面试题 - 5917873302142976的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- C#执行异步操作的几种方式比较和总结(转发:https://www.cnblogs.com/durow/p/4826653.html)
0x00 引言 之前写程序的时候在遇到一些比较花时间的操作例如HTTP请求时,总是会new一个Thread处理.对XxxxxAsync()之类的方法也没去了解过,倒也没遇到什么大问题.最近因为需求要求 ...
- uvm_hdl——DPI在UVM中的实现(四)
我们可以在uvm中实现HDL的后门访问,具体包括的function有uvm_hdl_check_path,uvm_hdl_deposit, uvm_hdl_force,uvm_hdl_release, ...
- jQuery_1_基础核心
jQuery代码风格:在jQuery程序中,不管是页面元素的选择还是内置的功能函数,都是以“$"来起始的. $(function(){}); / ...
- 三种zigbee网络架构详解
在万物互联的背景下,zigbee网络应用越加广泛,zigbee技术具有强大的组网能力,可以形成星型.树型和网状网,三种zigbee网络结构各有优势,可以根据实际项目需要来选择合适的zigbee网络结构 ...
- IOS 绘制基本图形(画文字、图片水印)
- (void)drawRect:(CGRect)rect { // Drawing code // [self test]; // 1.加载图片到内存中 UIImage *image = [UIIm ...
- Zend Studio 12.5.1原版安装破解
安装官方Zend Studio 12.5.1原版,关闭zend studio,然后将破解补丁com.zend.verifier_12.5.1.v20150514-2003.jar覆盖到 安装目录\pl ...
- pip 安装出现异常
MacBookPro:~ mac$ pip install numpy Collecting numpy Downloading numpy-1.13.1-cp35-cp35m-macosx_10_6 ...
- 第四章 用javascript和DOM去建立一个图片库
把整个图片库的浏览链接集中安排在你的图片库里,只在用户点击了这个主页里的某个图片链接时才把相应的图片传送给它. 代码如下: <body> <ul> <li> < ...
- Linux 开启关闭防火墙
开放防火墙端口添加需要监听的端口 /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT/sbin/iptables -I INPUT -p tcp ...
- Oracle grant connect, resource to user语句中的权限
博主在 Oracle 11g r2上测试(测试日期:2017.10.30): 用sys登陆到oracle中,执行以下两条语句: select * from role_sys_privs WHERE R ...