forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60365f4
commit 64e9203
Showing
4 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/// Source : https://leetcode.com/problems/clone-graph/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-14 | ||
|
||
#include <iostream> | ||
#include <stack> | ||
#include <vector> | ||
#include <unordered_map> | ||
|
||
using namespace std; | ||
|
||
/// Definition for undirected graph. | ||
struct UndirectedGraphNode { | ||
int label; | ||
vector<UndirectedGraphNode *> neighbors; | ||
UndirectedGraphNode(int x) : label(x) {}; | ||
}; | ||
|
||
/// Using Only One Stacks and HashMap from Node to Node | ||
/// Time Complexity: O(V+E) | ||
/// Space Complexity: O(V) | ||
class Solution { | ||
public: | ||
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { | ||
|
||
if(node == NULL) | ||
return NULL; | ||
|
||
UndirectedGraphNode* ret = new UndirectedGraphNode(node->label); | ||
stack<UndirectedGraphNode*> stack; | ||
stack.push(node); | ||
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> nodeMap; | ||
nodeMap[node] = ret; | ||
while(!stack.empty()){ | ||
UndirectedGraphNode* cur = stack.top(); | ||
stack.pop(); | ||
for(UndirectedGraphNode *next: cur->neighbors) { | ||
if (!nodeMap.count(next)) { | ||
nodeMap[next] = new UndirectedGraphNode(next->label); | ||
stack.push(next); | ||
} | ||
nodeMap[cur]->neighbors.push_back(nodeMap[next]); | ||
} | ||
} | ||
return ret; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/// Source : https://leetcode.com/problems/clone-graph/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-14 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_map> | ||
|
||
using namespace std; | ||
|
||
/// Definition for undirected graph. | ||
struct UndirectedGraphNode { | ||
int label; | ||
vector<UndirectedGraphNode *> neighbors; | ||
UndirectedGraphNode(int x) : label(x) {}; | ||
}; | ||
|
||
/// DFS | ||
/// Time Complexity: O(V+E) | ||
/// Space Complexity: O(V) | ||
class Solution { | ||
|
||
public: | ||
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { | ||
|
||
if(node == NULL) | ||
return NULL; | ||
|
||
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> nodeMap; | ||
return dfs(node, nodeMap); | ||
} | ||
|
||
private: | ||
UndirectedGraphNode *dfs(UndirectedGraphNode *node, | ||
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>& nodeMap){ | ||
|
||
if(nodeMap.count(node)) | ||
return nodeMap[node]; | ||
|
||
nodeMap[node] = new UndirectedGraphNode(node->label); | ||
for(UndirectedGraphNode* next: node->neighbors) | ||
nodeMap[node]->neighbors.push_back(dfs(next, nodeMap)); | ||
return nodeMap[node]; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |