《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二维数组的方式,其 ...
随机推荐
- 2017.10.28 QB模拟赛 —— 下午
题目链接 T1 按x值排序 遇到第二种牌插入 遇到第一种牌 查询<=y 的最小值 删除他 splay multiset cys大佬说 multiset就是不去重的set, #include &l ...
- Android(java)学习笔记62:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解
1. 先看看网路上的说法: android.intent.action.MAIN 决定应用程序最先启动的 Activity android.intent.category.LAUNCHER 决定应用程 ...
- 深搜,四色定理,(POJ1129)
题目链接:http://poj.org/problem?id=1129 解题报告: #include<iostream> #include<cstdio> #include&l ...
- 有权并查集,Poj(1988)
题目链接:http://poj.org/problem?id=1988 题目大意: 有n个从1到n编号的箱子,将每个箱子当做一个栈,对这些箱子进行p次操作,每次操作分别为以下两种之一: 输入 M x ...
- python学习接口测试(二)
.python接口之http请求 python的强大之处在于提供了很多的标准库以及第三库,本文介绍urllib 和第三库的requests. Urllib 定义了很多函数和类,这些函数和类能够帮助我们 ...
- 2017.10.13 Java中引用类型变量的创建及使用&循环高级写法
今日内容介绍 1.引用类型变量的创建及使用 2.流程控制语句之选择语句 3.流程控制语句之循环语句 4.循环高级 ###01创建引用类型变量公式 * A: 创建引用类型变量公式 ...
- (python)剑指Offer:数组中重复的数字
问题描述 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1, ...
- xrdp 安装后 WINDOWS远程登录出错
xrdp需要vnc作为基础服务, sudo apt-get install tightvncserver 树莓派上这个命令运行下再连就好了
- Java 类加载体系之 ClassLoader 双亲委托机制
Java 类加载体系之 ClassLoader 双亲委托机制 java 是一种类型安全的语言,它有四类称为安全沙箱机制的安全机制来保证语言的安全性,这四类安全沙箱分别是: 类加载体系 .class文件 ...
- sql查询字段值长度判断是否18位
SELECT * FROM 表名 WHERE LENGTH(字段)= OR 字段为null IS NULL OR 字段为空='' SELECT * FROM 表名 WHERE LENGTH(字段)&g ...