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 ...
随机推荐
- CUBRID学习笔记 8 复制数据库
1 export database 类似sqlserver的分离数据库 cubrid unloaddb demodb分离后生成三个文件 demodb_objects, demodb_indexe ...
- iOS - PassData
前言 页面传值: 复合传值 单例传值 userDefaults 传值 代理传值 Block/闭包传值 1.复合传值 复合 - 正向传值 1.接收方,头文件中创建可存放传递值的属性变量 2.发送方,包含 ...
- Web缓存的作用与类型
前言 Web缓存是指一个Web资源(如html页面,图片,js,数据等)存在于Web服务器和客户端(浏览器)之间的副本.缓存会根据进来的请求保存输出内容的副本:当下一个请求来到的时候,如果是相同的UR ...
- linux虚拟机安装
1.真实机第一次安装必须先搞f2进入boot从光盘启动,虚拟机不用 进入的时候五个选项Install or upgrade an existing system:安装或升级现有系统Install sy ...
- Tuning 简介
典型的不好的设计: 破坏了系统的可扩展性(韧性) Applications requiring significant concurrency management as user populatio ...
- 前端代码新写法——Zen Coding
是不是还在为html繁琐的标签写法而苦恼? 是不是还在枯燥的写尖括号? Zen Coding的到来将让前端编码不在繁琐.不再拘泥html的尖括号和一堆一堆的标签. 先看看ZenCoding的效果图. ...
- GIT过滤
git 创建 .gitignore 文件 建立项目过滤规则 创建 .gitignore 随意设置想跟踪哪些文件 和不跟踪哪些文件. 1.在项目根目录下建立 .gitignore 文件 2. .gi ...
- Android布局_相对布局RelativeLayout
一.RelativeLayout(相对布局)概述 RelativeLayout是相对布局控件,它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列 二.RelativeLayo ...
- 解决SQL命令行回退的问题
场景 在linux或者aix上安装后Oracle后,在SQL命令行下无法通过键盘的退格键回退,如下 解决方法 安装软件 # rpm -ivh rlwrap-0.41-1.el6.x86_64.rpm ...
- LinuxShell脚本攻略--第八章 当个好管家
监视磁盘的使用情况 $ du file1.txt file2.txt $ du -a file_or_dir #-a递归输出指定目录的所有文件统计 $ du file_or_dir #这只是显示子目录 ...