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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  4. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  8. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  9. 《Cracking the Coding Interview》——第17章:普通题——题目13

    2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...

  10. 《Cracking the Coding Interview》——第13章:C和C++——题目10

    2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...

随机推荐

  1. 笨办法学Python(三十四)

    习题 34: 访问列表的元素 列表的用处很大,但只有你能访问里边的内容时它才能发挥出作用来.你已经学会了按顺序读出列表的内容,但如果你要得到第 5 个元素该怎么办呢?你需要知道如何访问列表中的元素.访 ...

  2. 【CSS古话今说】-- 01.神奇的CSS-BFC在实战中的应用

    文章首发于掘金 BFC(Block Formatting Context)是Web页面中盒模型布局的CSS渲染模式.它的定位体系属于常规文档流. 想要实现一个BFC布局需要满足以下条件之一: 1.fl ...

  3. 将TIMESTAMP类型的差值转化为秒的方法

    两个TIMESTAMP之差得到的是INTERVAL类型,而有时我们只需要得到两个时间相差的秒数,如果变成INTERVAL之后,想要获取这个值会非常麻烦. 比较常见的方法是使用EXTRACT来抽取获得的 ...

  4. redis string类型

  5. Spring任务执行器(TaskExecutor)

    Spring任务执行器(TaskExecutor)    Spring通州任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor可实现一个基于线 ...

  6. 【洛谷P1314】[NOIP2011]聪明的质监员

    聪明的质监员 题目链接:https://www.luogu.org/problemnew/show/P1314 Y(W)随W的值增大而减小 二分W的值,找到最小的W使得Y(W)>S: 比较Y(W ...

  7. hadoop分类输出

    import org.apache.hadoop.io.Text; import java.io.IOException;import java.util.Iterator;import java.u ...

  8. iOS圆角view的Swift实现(利用Core Graphics绘制)

    iOS圆角view的Swift实现(利用Core Graphics绘制) 因为app的列表用用到了圆形图片的头像,所以去探究并思考了一下这个问题.首先这个问题有两个方向的解决方案: 把图片弄成圆形的. ...

  9. 表单验证实现React-router跳转

    方法一:broserHistory.push handleSubmit(e){ e.preventDefault(); const path = '/demo'; broserHistory.push ...

  10. FAT32中文版分析+补充(1)

    概述 起先所有的FAT文件系统都是为IBM PC机器而设计的,这说明了一个重要的问题:FAT文件系统在磁盘上的数据是用“小端”(Little Endian)结构存储的.我们使用4个8-bit的字节—— ...