《Cracking the Coding Interview》——第13章:C和C++——题目7
2014-04-25 20:18
题目:给定一个Node结构体,其中包含数据成员和两个Node*指针指向其他两个Node结构(还不如直接说这是个图呢)。给你一个Node指针作为参数,请做一份深拷贝作为结果返回。
解法:BFS搞定,需要检测重复节点以防止死循环,用一个哈希表可以做大。这样肯定只能找出一个完整的连通分量,其他连通分量的节点是无法检测到的。下面的代码其实是我在做leetcode时写的。
代码:
// 13.7 Given a pointer to a Node strcut, return a deep copy of whatever you can find with it.
// Answer:
// The following code is actually my solution to the leetcode problem, Clone Graph.
// They are different problems, but almost the same idea of BFS, mapping and deep copy.
#include <queue>
#include <vector>
#include <unordered_map>
using namespace std;
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == nullptr) {
return nullptr;
} UndirectedGraphNode *cur, *nei;
int i, j;
int nc;
int ix, iy;
int nsize; nc = ;
qq.push(node);
while (!qq.empty()) {
cur = qq.front();
qq.pop();
if (um.find(cur) == um.end()) {
um[cur] = nc++;
graph.push_back(vector<int>());
labels.push_back(cur->label);
nodes_checked.push_back(false);
}
ix = um[cur];
if (nodes_checked[ix]) {
continue;
}
nsize = (int)cur->neighbors.size();
for (i = ; i < nsize; ++i) {
nei = cur->neighbors[i];
if (um.find(nei) == um.end()) {
um[nei] = nc++;
labels.push_back(nei->label);
graph.push_back(vector<int>());
nodes_checked.push_back(false);
}
iy = um[nei];
if (!nodes_checked[iy]) {
qq.push(nei);
}
graph[ix].push_back(iy);
}
nodes_checked[ix] = true;
} new_nodes.clear();
for (i = ; i < nc; ++i) {
new_nodes.push_back(new UndirectedGraphNode(labels[i]));
}
for (i = ; i < nc; ++i) {
nsize = (int)graph[i].size();
for (j = ; j < nsize; ++j) {
new_nodes[i]->neighbors.push_back(new_nodes[graph[i][j]]);
}
}
cur = new_nodes[];
while (!qq.empty()) {
qq.pop();
}
um.clear();
new_nodes.clear();
for (i = ; i < (int)graph.size(); ++i) {
graph[i].clear();
}
graph.clear();
labels.clear();
nodes_checked.clear(); return cur;
}
private:
queue<UndirectedGraphNode *> qq;
unordered_map<UndirectedGraphNode *, int> um;
vector<int> labels;
vector<bool> nodes_checked;
vector<UndirectedGraphNode *> new_nodes;
vector<vector<int> > graph;
};
《Cracking the Coding Interview》——第13章:C和C++——题目7的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...
随机推荐
- Uva 10820 交表
题目链接:https://uva.onlinejudge.org/external/108/10820.pdf 题意: 对于两个整数 x,y,输出一个函数f(x,y),有个选手想交表,但是,表太大,需 ...
- 【[ZJOI2015]诸神眷顾的幻想乡】
题目 听说这是广义\(SAM\)的板子 看来对于广义\(SAM\)我也就只会板子了 叶子数很少,所以可以枚举每一个叶子节点作为根建一遍\(Trie\)树 只需要对\(Trie\)树建出\(SAM\)就 ...
- CentOS 6下PXE+Kickstart无人值守安装操作系统
一.简介1.1 什么是PXEPXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作 ...
- 【转】Activity生命周期详解
三个循环 提供两个关于Activity的生命周期模型图示帮助理解: 图1 图2 从图2所示的Activity生命周期 ...
- jQuery 遍历函数包括了用于筛选、查找和串联元素的方法。
jQuery 参考手册 - 遍历 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集合中. .children() 获得匹配元素集合中每个 ...
- ceph-对象存储
ceph对象存储 作为文件系统的磁盘,操作系统不能直接访问对象存储.相反,它只能通过应用程序级别的API访问.ceph是一种分布式对象存储系统,通过ceph对象网关提供对象存储接口,也称为RADOS网 ...
- undefined reference to 'dlopen';undefined reference to 'dlclose';undefined reference to 'dlerror'等问题
在linux下,编译链接的时候,经常会遇到这样一个问题,undefined reference to.....,引起这个问题的原因在于在链接的时候缺少选项.下面举几个例子,并给出解决办法. 1. u ...
- PEP8 常用规范
PEP8 常用规范 完整的规范移步这里两个传送门 pep8规范 官方文档:https://www.python.org/dev/peps/pep-0008/ PEP8中文翻译:http://www.c ...
- ant Design表单验证笔记
1.pattern正则验证 <Col md={12} sm={24}> <FormItem {...formItemLayout} label="班数"> ...
- WPF与Silverlight对比
1.WPF中控件的肤色可以直接:telerik:StyleManager.Theme=”XXXXX”,不用再导入肤色的dll包.可Silverlight使用系统肤色时,要导入肤色的dll包. WPF引 ...