From 64e9203548e0cafa422af3e582e1a1c17535e2c6 Mon Sep 17 00:00:00 2001 From: liuyubobobo Date: Fri, 14 Sep 2018 18:12:29 -0700 Subject: [PATCH] 0133 new algo added. --- 0133-Clone-Graph/cpp-0133/CMakeLists.txt | 2 +- 0133-Clone-Graph/cpp-0133/main.cpp | 2 +- 0133-Clone-Graph/cpp-0133/main2.cpp | 53 ++++++++++++++++++++++++ 0133-Clone-Graph/cpp-0133/main3.cpp | 51 +++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 0133-Clone-Graph/cpp-0133/main2.cpp create mode 100644 0133-Clone-Graph/cpp-0133/main3.cpp diff --git a/0133-Clone-Graph/cpp-0133/CMakeLists.txt b/0133-Clone-Graph/cpp-0133/CMakeLists.txt index 726f8434..06e51fa4 100644 --- a/0133-Clone-Graph/cpp-0133/CMakeLists.txt +++ b/0133-Clone-Graph/cpp-0133/CMakeLists.txt @@ -3,5 +3,5 @@ project(cpp_0133) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES main.cpp) +set(SOURCE_FILES main3.cpp) add_executable(cpp_0133 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0133-Clone-Graph/cpp-0133/main.cpp b/0133-Clone-Graph/cpp-0133/main.cpp index 12d52878..10890562 100644 --- a/0133-Clone-Graph/cpp-0133/main.cpp +++ b/0133-Clone-Graph/cpp-0133/main.cpp @@ -16,7 +16,7 @@ struct UndirectedGraphNode { UndirectedGraphNode(int x) : label(x) {}; }; -/// Using Stacks +/// Using Two Stacks and HashMap from label to Node /// Time Complexity: O(V+E) /// Space Complexity: O(V) class Solution { diff --git a/0133-Clone-Graph/cpp-0133/main2.cpp b/0133-Clone-Graph/cpp-0133/main2.cpp new file mode 100644 index 00000000..7010b949 --- /dev/null +++ b/0133-Clone-Graph/cpp-0133/main2.cpp @@ -0,0 +1,53 @@ +/// Source : https://leetcode.com/problems/clone-graph/description/ +/// Author : liuyubobobo +/// Time : 2018-09-14 + +#include +#include +#include +#include + +using namespace std; + +/// Definition for undirected graph. +struct UndirectedGraphNode { + int label; + vector 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 stack; + stack.push(node); + unordered_map 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; +} \ No newline at end of file diff --git a/0133-Clone-Graph/cpp-0133/main3.cpp b/0133-Clone-Graph/cpp-0133/main3.cpp new file mode 100644 index 00000000..56f106c2 --- /dev/null +++ b/0133-Clone-Graph/cpp-0133/main3.cpp @@ -0,0 +1,51 @@ +/// Source : https://leetcode.com/problems/clone-graph/description/ +/// Author : liuyubobobo +/// Time : 2018-09-14 + +#include +#include +#include + +using namespace std; + +/// Definition for undirected graph. +struct UndirectedGraphNode { + int label; + vector 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 nodeMap; + return dfs(node, nodeMap); + } + +private: + UndirectedGraphNode *dfs(UndirectedGraphNode *node, + unordered_map& 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; +} \ No newline at end of file