`
jgsj
  • 浏览: 959423 次
文章分类
社区版块
存档分类
最新评论

LeetCode Copy List with Random Pointer 分析解答

 
阅读更多

Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

首先搞清楚什么是浅拷贝和深拷贝:

浅拷贝,把一个变量中的数据地址直接给另一个变量,删除其中任何一个变量,另一个变量中就没有了地址数据。
深拷贝,创建一个新的空间保存这个变量的值,所以,当删除任何一个变量时,就不会出错,因为它们的地址不同

如下列:

#include<iostream>
#include<cstdlib>
#include<memory>
int main()
 {
         int* p1 = new int(1);
         
         std::auto_ptr<int> p(new int(*p1) );
         int* p2 = p.get();
           
         delete p1;                                                                                                
         std::cout<<*p2<<std::endl;                                                                                                                                                                                                                                                                                                            
         system("PAUSE"); 
         return 0;
 } 


下面是用map来写的一个程序,第一次提交就通过了,暗暗高兴中。

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
	RandomListNode *copyRandomList(RandomListNode *head) 
	{
		if(head == nullptr) return nullptr;
		map<RandomListNode *, RandomListNode *> nodesmap;
		nodesmap = copyList(head);
		randomPtr(nodesmap);
		return nodesmap[head];
	}

	map<RandomListNode *, RandomListNode*> copyList(RandomListNode *head)
	{
		RandomListNode * node = head;
		map<RandomListNode *, RandomListNode*> nodesmap;
		RandomListNode *chead = new RandomListNode(*node);
		nodesmap[node] = chead;
		RandomListNode *cur = chead;
		while (node->next != nullptr)
		{
			RandomListNode *ctemp = new RandomListNode(*(node->next));
			nodesmap[node->next] = ctemp;
			cur->next = ctemp;
			cur = ctemp;
			node = node->next;
		}
		cur->next = nullptr;
		return nodesmap;
	}

	void randomPtr(map<RandomListNode *, RandomListNode*> &nodesmap)
	{
		auto iter = nodesmap.begin();
		for(; iter != nodesmap.end(); iter++)
		{
			iter->second->random = nodesmap[iter->first->random];
		}
	}
};


和 clone graph这道题的算法一样,算是其简易版本。

http://blog.csdn.net/kenden23/article/details/18624191

//2014-2-19 update
	RandomListNode *copyRandomList(RandomListNode *head) 
	{
		unordered_map<int, RandomListNode *> ump_ir;
		return copyRL(head, ump_ir);
	}
	RandomListNode *copyRL(RandomListNode *h, unordered_map<int, RandomListNode*>&ump_ir)
	{//&ump_ir 写出 ump_ir导致出错
		if (!h) return nullptr;
		if (ump_ir.count(h->label)) return ump_ir[h->label];

		RandomListNode *nh = new RandomListNode(h->label);
		ump_ir[h->label] = nh;
		nh->next = copyRL(h->next, ump_ir);
		//nh->random = copyRL(h->random, ump_ir);
		if (h->random) nh->random = ump_ir[h->random->label];
		return nh;
	}


//2014-2-19_2 update
	RandomListNode *copyRandomList(RandomListNode *head) 
	{
		unordered_map<RandomListNode *, RandomListNode *> ump_ir;
		return copyRL(head, ump_ir);
	}
	RandomListNode *copyRL(RandomListNode *h, 
		unordered_map<RandomListNode *, RandomListNode *> &ump_ir)
	{
		if (!h) return nullptr;
		if (ump_ir.count(h)) return ump_ir[h];

		RandomListNode *nh = new RandomListNode(h->label);
		ump_ir[h] = nh;
		nh->next = copyRL(h->next, ump_ir);
		nh->random = ump_ir[h->random];//copyRL(h->random, ump_ir);
		return nh;
	}



分享到:
评论

相关推荐

    leetcode中文版-LeetCode:力码

    leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...

    两两认识leetcode-copy-list-with-random-pointer:使用随机指针复制链表

    两两认识leetcode 使用随机指针复制链表 给出一个链表,使得每个节点都包含一个额外的随机指针,该指针可以指向链表中的任何节点或为空。 返回列表的深层副本。 链表在输入/输出中表示为 n 个节点的列表。 每个节点...

    dna匹配leetcode-leetcode:leetcode刷题

    dna匹配 leetcode leetcode刷题--C++ 哈希表 Longest Substring ...Pointer 单链表 map Max Points on a Line 斜率 map, int&gt; Fraction to Recurring Decimal map long long 正负号 Repeated DNA S

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    多线程leetcode-leetcode-java:leetcode上的题解,基于java语言

    多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。...Copy List with Random Pointer Building H2O Fizz Buzz Multithreaded hard Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water

    leetcode题库-pyshua:这是一个Python的编码判断系统

    leetcode题库 pyshua Python 算法题练习 用法: python Judge.py library problem 例子: python Judge.py leetcode TwoSum 如何贡献: 收录题库 LeetCode (还有4题未录入, 分别为 LRU Cache, Copy List with Random ...

    LeetCode:LeetCode解决方案

    preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-list-cycle动态规划word-break-ii动态规划word-break链表copy-list-with-random-pointer复杂度single-number-ii复杂度single-number动态规划

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归...

    cpp-算法精粹

    Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary ...

    lrucacheleetcode-CPPS:CPPS

    List_with_Random_Pointer.py) + 16 散列 + 17 散列 [A](./hash_table/Insert_Delete_GetRandom_O(1).py) + 18 大批 + 19 二分查找 + 20 DP —— 21 堆 —— 22 散列 + 23 DP + 24 DP —— 25 DP + 26 堆 + 27 堆 ...

Global site tag (gtag.js) - Google Analytics