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. 修改Linux中发送邮件中附件大小的限制

    方法一: 在命令中设定postfix的message_size_limit值 (但系统重启后会失效) postconf -e "message_size_limit = 20480000&q ...

  2. ubuntu16.04安装中文输入法(转)

    转自: https://zhidao.baidu.com/question/619127469641961052.html ubuntu没有预装中文输入法,需要自己安装 sudo apt instal ...

  3. 【JavaScript 封装库】BETA 4.0 测试版发布!

    /* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...

  4. .net core 2.0以上版本加载appsettings.json

    这里需要的一个关键类: Microsoft.Extensions.Configuration; 可以从nuget包获得 如果缺少该类,会造成无法实例化调用方法: ConfigurationBuilde ...

  5. ABI and compiler

    http://stackoverflow.com/questions/2171177/what-is-application-binary-interface-abi ABIs cover detai ...

  6. 为什么实例没有prototype属性?什么时候对象会有prototype属性呢?

    为什么实例没有prototype属性?什么时候对象会有prototype属性呢? javascript loudou 1月12日提问 关注 9 关注 收藏 6 收藏,554 浏览 问题对人有帮助,内容 ...

  7. 奇异值分解(SVD)原理及应用

    一.奇异值与特征值基础知识: 特征值分解和奇异值分解在机器学习领域都是属于满地可见的方法.两者有着很紧密的关系,我在接下来会谈到,特征值分解和奇异值分解的目的都是一样,就是提取出一个矩阵最重要的特征. ...

  8. python_4_interaction

    #1(方法1)尽量不用这种拼接法,效率低下,占用内存多 name=input("name:") age=input('age:') job=input('job:') salary ...

  9. Linux中的/etc/nologin问题

    /etc/nologin 文件给系统管理员提供了在 Linux 系统维护期间禁止用户登陆的方式. 如果系统中存在 /etc/nologin 文件那么普通用户登陆就会失败. 这是一种提高安全性和防止数据 ...

  10. 将你的静态工程用Grunt管理

    最近写了一个静态页面,写完之后都会上传到静态服务器上.但是我遇到一个问题,就是每次修改文件后就要重新找一些代码压缩网站去压缩静态文件.有没有什么办法能够自动化的处理呢?答案当然是肯定的. 我们可以借用 ...