https://github.com/AnnieKim/ITint5/blob/master/046_%E5%A4%8D%E5%88%B6%E6%9C%89%E5%90%91%E5%9B%BE.cpp
复制一个有向图。输入是有向图中的一个结点指针,返回复制的图中对应的结点指针。
Solution: 我在facebook电面时也遇到过这个题目,deep copy一个graph。
*/
/*
有向图中结点的定义为:
struct GraphNode {
int data;
vector<GraphNode*> neighbors;
GraphNode(int data) : data(data) {}
};
*/
/*
方案 1:DFS。
*/
typedef unordered_map<GraphNode *, GraphNode *> MAP;
GraphNode *cloneGraphRe(GraphNode *node, MAP &exist) {
if (!node) return NULL;
if (exist.find(node) != exist.end())
return exist[node];
GraphNode *newNode = new GraphNode(node->data);
exist[node] = newNode;
for (int i = 0; i < node->neighbors.size(); ++i)
newNode->neighbors.push_back(cloneGraphRe(node->neighbors[i], exist));
return newNode;
}
GraphNode *cloneGraph(GraphNode *node) {
MAP exist;
return cloneGraphRe(node, exist);
}
/*
方案 2:BFS。
*/
typedef unordered_map<GraphNode *, GraphNode *> MAP;
GraphNode *cloneGraph(GraphNode *node) {
if (!node) return NULL;
MAP exist;
exist[node] = new GraphNode(node->data);
queue<GraphNode *> q;
q.push(node);
while (!q.empty())
{
GraphNode *orig = q.front();
GraphNode *newNode = exist[orig];
q.pop();
for (int i = 0; i < orig->neighbors.size(); ++i)
{
if (exist.find(orig->neighbors[i]) != exist.end()) {
newNode->neighbors.push_back(exist[orig->neighbors[i]]);
continue;
}
GraphNode *newNeighbor = new GraphNode(orig->neighbors[i]->data);
newNode->neighbors.push_back(newNeighbor);
exist[orig->neighbors[i]] = newNeighbor;
q.push(orig->neighbors[i]);
}
}
return exist[node];
}
https://github.com/ralphite/ITint5-Answers-Java/blob/master/%E5%A4%8D%E5%88%B6%E6%9C%89%E5%90%91%E5%9B%BE.java
public GraphNode cloneGraph(GraphNode node) {
if(node==null) return null;
//dfs and add cloned node to it's neighbors
HashSet<GraphNode> set = new HashSet<GraphNode>();
dfs(node, set);
//dfs again to setup the cloned nodes neighbors
set.clear();
dfs2(node, set);
GraphNode newNode = node.neighbors.get(node.neighbors.size()-1);
//dfs3 to remove the cloned node from the neighbors list
//optional if we can change node's structure
set.clear();
dfs3(node, set);
return newNode;
}
private static void dfs(GraphNode node, HashSet<GraphNode> set) {
if(node==null) return;
if(set.contains(node)) return;
set.add(node);
for(GraphNode n : node.neighbors) {
dfs(n, set);
}
node.neighbors.add(new GraphNode(node.data));
}
private static void dfs2(GraphNode node, HashSet<GraphNode> set) {
if(node==null) return;
if(set.contains(node)) return;
set.add(node);
GraphNode newNode = node.neighbors.get(node.neighbors.size()-1);
for(int i = 0; i < node.neighbors.size()-1; i++) {
GraphNode n = node.neighbors.get(i);
newNode.neighbors.add(n.neighbors.get(n.neighbors.size()-1));
dfs2(n, set);
}
}
private static void dfs3(GraphNode node, HashSet<GraphNode> set) {
if(node==null) return;
if(set.contains(node)) return;
set.add(node);
node.neighbors.remove(node.neighbors.size()-1);
for(int i = 0; i < node.neighbors.size()-1; i++) {
GraphNode n = node.neighbors.get(i);
dfs3(n, set);
}
}
复制一个有向图。输入是有向图中的一个结点指针,返回复制的图中对应的结点指针。
Solution: 我在facebook电面时也遇到过这个题目,deep copy一个graph。
*/
/*
有向图中结点的定义为:
struct GraphNode {
int data;
vector<GraphNode*> neighbors;
GraphNode(int data) : data(data) {}
};
*/
/*
方案 1:DFS。
*/
typedef unordered_map<GraphNode *, GraphNode *> MAP;
GraphNode *cloneGraphRe(GraphNode *node, MAP &exist) {
if (!node) return NULL;
if (exist.find(node) != exist.end())
return exist[node];
GraphNode *newNode = new GraphNode(node->data);
exist[node] = newNode;
for (int i = 0; i < node->neighbors.size(); ++i)
newNode->neighbors.push_back(cloneGraphRe(node->neighbors[i], exist));
return newNode;
}
GraphNode *cloneGraph(GraphNode *node) {
MAP exist;
return cloneGraphRe(node, exist);
}
/*
方案 2:BFS。
*/
typedef unordered_map<GraphNode *, GraphNode *> MAP;
GraphNode *cloneGraph(GraphNode *node) {
if (!node) return NULL;
MAP exist;
exist[node] = new GraphNode(node->data);
queue<GraphNode *> q;
q.push(node);
while (!q.empty())
{
GraphNode *orig = q.front();
GraphNode *newNode = exist[orig];
q.pop();
for (int i = 0; i < orig->neighbors.size(); ++i)
{
if (exist.find(orig->neighbors[i]) != exist.end()) {
newNode->neighbors.push_back(exist[orig->neighbors[i]]);
continue;
}
GraphNode *newNeighbor = new GraphNode(orig->neighbors[i]->data);
newNode->neighbors.push_back(newNeighbor);
exist[orig->neighbors[i]] = newNeighbor;
q.push(orig->neighbors[i]);
}
}
return exist[node];
}
https://github.com/ralphite/ITint5-Answers-Java/blob/master/%E5%A4%8D%E5%88%B6%E6%9C%89%E5%90%91%E5%9B%BE.java
public GraphNode cloneGraph(GraphNode node) {
if(node==null) return null;
//dfs and add cloned node to it's neighbors
HashSet<GraphNode> set = new HashSet<GraphNode>();
dfs(node, set);
//dfs again to setup the cloned nodes neighbors
set.clear();
dfs2(node, set);
GraphNode newNode = node.neighbors.get(node.neighbors.size()-1);
//dfs3 to remove the cloned node from the neighbors list
//optional if we can change node's structure
set.clear();
dfs3(node, set);
return newNode;
}
private static void dfs(GraphNode node, HashSet<GraphNode> set) {
if(node==null) return;
if(set.contains(node)) return;
set.add(node);
for(GraphNode n : node.neighbors) {
dfs(n, set);
}
node.neighbors.add(new GraphNode(node.data));
}
private static void dfs2(GraphNode node, HashSet<GraphNode> set) {
if(node==null) return;
if(set.contains(node)) return;
set.add(node);
GraphNode newNode = node.neighbors.get(node.neighbors.size()-1);
for(int i = 0; i < node.neighbors.size()-1; i++) {
GraphNode n = node.neighbors.get(i);
newNode.neighbors.add(n.neighbors.get(n.neighbors.size()-1));
dfs2(n, set);
}
}
private static void dfs3(GraphNode node, HashSet<GraphNode> set) {
if(node==null) return;
if(set.contains(node)) return;
set.add(node);
node.neighbors.remove(node.neighbors.size()-1);
for(int i = 0; i < node.neighbors.size()-1; i++) {
GraphNode n = node.neighbors.get(i);
dfs3(n, set);
}
}