133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
Nodes are labeled uniquely.
We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}.
The graph has a total of three nodes, and therefore contains three parts as separated by #.
- First node is labeled as
0. Connect node0to both nodes1and2. - Second node is labeled as
1. Connect node1to node2. - Third node is labeled as
2. Connect node2to node2(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(node == NULL)
return NULL;
UndirectedGraphNode *cloneNode = new UndirectedGraphNode(node->label);
map<UndirectedGraphNode*, UndirectedGraphNode*> m;
m[node] = cloneNode;
queue<UndirectedGraphNode*> q;
q.push(node);
while(!q.empty())
{
UndirectedGraphNode *t = q.front();
q.pop();
for(int i = ; i < t->neighbors.size(); i++)
{
UndirectedGraphNode *neighbor = t->neighbors[i];
if(m.find(neighbor) == m.end())
{
UndirectedGraphNode *p = new UndirectedGraphNode(neighbor->label);
m[neighbor] = p;
m[t]->neighbors.push_back(p);
q.push(neighbor);
}
else
{
m[t]->neighbors.push_back(m[neighbor]);
}
}
}
return cloneNode;
}
};
138. 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.
/**
* 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) {}
* };
*/
(1)
* Consider we have a linked list as below:
*
* node1->random = node2;
* node2->random = node1;
* node3->random = node1;
*
* +-------------+
* | v
* +-------+ +-------+ +-------+
* | node1 |----> node2 |----> node3 |--->NULL
* +-------+ +-------+ +-------+
* ^ ^ | |
* | +-----------+ |
* +--------------------------+
*
*
* To copy the list,
*
* 1) We insert a new node for each node's next position
*
*
* +-------------------------+
* | v
* +--+----+ +-----+ +-------+ +-----+ +-------+ +-----+
* | node1 |---> | NEW |----> node2 |---> | NEW |----> node3 |---> | NEW | ---> NULL
* +-------+ +-----+ +---+---+ +-----+ +--+----+ +-----+
* ^ ^ | |
* | +-----------------------+ |
* +--------------------------------------------------+
*
* 2) Then, we can construt the new node's random pointer:
*
* (node1->next) -> random = (node1->random) -> next;
*
* 3) After we take out all of the "NEW" node to finish the copy.
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *p = NULL, *h=NULL, *t=NULL;
if (head == NULL){
return NULL;
}
//creat a new node for each node and insert its next
p = head;
while ( p != NULL){
RandomListNode *node = new RandomListNode(p->label);
node->next = p->next;
p->next = node;
p = node->next;
}
//copy random pointer for each new node
p = head;
while (p != NULL){
if (p->random != NULL){
p->next->random = p->random->next;
}
p = p->next->next;
}
//break to two list
p = head;
h = t = head->next;
while ( p != NULL ){
p->next = p->next->next;
if (t->next!=NULL){
t->next = t->next->next;
}
p = p->next;
t = t->next;
}
return h;
}
};
(2)
* Considering we have a link as below:
*
*
* +-------------+
* | v
* +-------+ +-------+ +-------+
* | node1 |----> node2 |----> node3 |--->NULL
* +-------+ +-------+ +-------+
* ^ ^ | |
* | +-----------+ |
* +--------------------------+
*
* 1) Using a map to store each node's random pointer's position
*
* map[node1->random] = 1;
* map[node2->random] = 0;
* map[node3->random] = 0;
*
* 2) Clone the linked list (only consider the next pointer)
*
* new1 --> new2 --> new3 --> NULL
*
* 3) Using an array to strore the order of the cloned linked-list
*
* v[0] = &new1
* v[1] = &new2
* v[2] = &new3
*
* 4) Then we can clone the random pointers.
*
* new->random = v [ map[node->random] ]
class MySolution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *p = NULL, *h=NULL, *t=NULL;
//using a map to store the random pointer's postion.
map<RandomListNode*, int> m;
//construct the map
int pos =;
for ( p = head; p != NULL; p = p->next, pos++){
m[p] = pos;
}
//clone the linked list (only consider the next pointer)
//and using a vector to store each node's postion.
vector<RandomListNode*> v;
for (p = head; p != NULL; p = p->next){
RandomListNode *node = new RandomListNode(p->label);
v.push_back(node);
if (h==NULL){
h = t = node;
}else{
t->next = node;
t = node;
}
}
//p => source link head
//t => new link head
//move the p and t synchronously, and
// t->random = vector[ map[p->random] ];
for (t=h, p = head; t!=NULL && p!= NULL; p=p->next, t=t->next){
if (p->random!=NULL) {
pos = m[p->random];
t->random = v[pos];
}
}
return h;
}
};
133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表的更多相关文章
- [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 ...
- 138. Copy List with Random Pointer (Graph, Map; DFS)
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 ...
- 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 poi ...
- 138. Copy List with Random Pointer (not do it by myself)
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复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
随机推荐
- XUnit的使用
安装: 通过NuGet获取XUnit的类库 实战使用 https://github.com/kerryjiang/SuperSocket.ProtoBase/tree/master/test
- Shifting List Item Values From One List To Another In Oracle Forms
Suppose you have two T-List items in form and you want to shift element values from one list to ano ...
- 【VB6笔记-01】 读取Excel绑定到DataGrid
Private Sub cmdOpen_Click() CommonDialog1.Filter = "Excel???t(*.xlsx)|*.xlsx" CommonDialog ...
- So easy Webservice 4.Java方式访问WebService(使用jdk1.6以上 wsimport命令)
1.选中要调用的服务单击”服务说明” 2.获取wsdl文件.使用JDK1.6以上的版本的wsimport命令 a) 例如选中:http://webservice.webxml.com.cn/WebSe ...
- hdu 2196 Computer 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- hdu 4163 Stock Prices 水
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...
- XAF应用开发教程(五)验证模块
数据验证是应用程序开发中使用频率最高的功能模块,本节详细介绍一下XAF中如何使用验证模块. XAF 验证模块内置了下面的一些验证规则: 验证规则类型 说明 RuleCombinationOfPrope ...
- Yii javascript 的结合 账号禁用 激活 的设置。
2014-02-16 控制器中的代码: public function actionUpdown(){ //print_r($_POST);die(); if(Buser::model()->u ...
- Scrum Meeting---Seven(2015-11-2)
今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 完成了服务器实现 5h 服务器与客户端连接测试 4h 胡亚坤 客户端与服务器端的通信 2h 客户端与 ...
- iOS - Swift Subscript 下标脚本
1.Subscript 下标脚本允许你通过在实例后面的方括号中传入一个或者多个的索引值来对实例进行访问和赋值.语法类似于实例方法和计算型属性的混合.与定义实例方法类似,定义下标脚本使用 subscri ...