From d45831418492267f750c25be334d2c263763d977 Mon Sep 17 00:00:00 2001 From: Liam Huang Date: Fri, 12 Oct 2018 08:56:57 +0800 Subject: [PATCH 1/3] [10_recursive] one_two_step, done. --- c-cpp/10_recursive/.gitkeep | 0 c-cpp/10_recursive/one_two_step.cc | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 c-cpp/10_recursive/.gitkeep create mode 100644 c-cpp/10_recursive/one_two_step.cc diff --git a/c-cpp/10_recursive/.gitkeep b/c-cpp/10_recursive/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/c-cpp/10_recursive/one_two_step.cc b/c-cpp/10_recursive/one_two_step.cc new file mode 100644 index 0000000..561c32b --- /dev/null +++ b/c-cpp/10_recursive/one_two_step.cc @@ -0,0 +1,26 @@ +#include +#include + +class SolutionOFOneTwoStep { + private: + static std::unordered_map result_; + + public: + size_t operator()(size_t steps) { + if (result_.end() != result_.find(steps)) { + return result_[steps]; + } else { + size_t res = operator()(steps - 1) + operator()(steps - 2); + result_.insert({steps, res}); + return res; + } + } +}; + +std::unordered_map + SolutionOFOneTwoStep::result_ = {{1, 1}, {2, 2}}; + +int main() { + std::cout << SolutionOFOneTwoStep()(10) << std::endl; + return 0; +} From 07297f8fb4f47b9fd8eab1a9a7e6b899e765f57f Mon Sep 17 00:00:00 2001 From: Liam Huang Date: Fri, 12 Oct 2018 09:00:30 +0800 Subject: [PATCH 2/3] [10_recursive] one_two_step, applying iterator. --- c-cpp/10_recursive/one_two_step.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/c-cpp/10_recursive/one_two_step.cc b/c-cpp/10_recursive/one_two_step.cc index 561c32b..7ae239f 100644 --- a/c-cpp/10_recursive/one_two_step.cc +++ b/c-cpp/10_recursive/one_two_step.cc @@ -7,8 +7,9 @@ class SolutionOFOneTwoStep { public: size_t operator()(size_t steps) { - if (result_.end() != result_.find(steps)) { - return result_[steps]; + auto iter = result_.find(steps); + if (result_.end() != iter) { // found. + return iter->second; } else { size_t res = operator()(steps - 1) + operator()(steps - 2); result_.insert({steps, res}); From 5a374e03f5be2f4b0d27613705b6e4046b615363 Mon Sep 17 00:00:00 2001 From: Liam Huang Date: Fri, 12 Oct 2018 17:46:51 +0800 Subject: [PATCH 3/3] [10_recursive] one_two_step, nonrecursive version, done. --- c-cpp/10_recursive/one_two_step.cc | 52 +++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/c-cpp/10_recursive/one_two_step.cc b/c-cpp/10_recursive/one_two_step.cc index 7ae239f..11159c7 100644 --- a/c-cpp/10_recursive/one_two_step.cc +++ b/c-cpp/10_recursive/one_two_step.cc @@ -6,7 +6,13 @@ class SolutionOFOneTwoStep { static std::unordered_map result_; public: - size_t operator()(size_t steps) { + enum class POLICY { + RECURSIVE, + NONRECURSIVE + }; + + private: + size_t recursive(size_t steps) { auto iter = result_.find(steps); if (result_.end() != iter) { // found. return iter->second; @@ -16,12 +22,50 @@ class SolutionOFOneTwoStep { return res; } } + size_t nonrecursive(size_t steps) { + auto iter = result_.find(steps); + if (result_.end() != iter) { // found. + return iter->second; + } else { + size_t start; + for (start = steps; start != 2 and result_.end() == result_.find(start); --start) {} + for (size_t i = start; i != steps; ++i) { + result_.insert({i + 1, result_[i - 1] + result_[i]}); + } + return result_[steps]; + } + } + + public: + size_t operator()(size_t steps, const POLICY policy = POLICY::RECURSIVE) { + if (policy == POLICY::RECURSIVE) { + return recursive(steps); + } else if (policy == POLICY::NONRECURSIVE) { + return nonrecursive(steps); + } + } + static void debug() { + for (auto kv : result_) { + std::cout << kv.first << ' ' << kv.second << std::endl; + } + std::cout << std::endl; + } }; -std::unordered_map - SolutionOFOneTwoStep::result_ = {{1, 1}, {2, 2}}; +std::unordered_map SolutionOFOneTwoStep::result_ = {{1, 1}, {2, 2}}; int main() { - std::cout << SolutionOFOneTwoStep()(10) << std::endl; + SolutionOFOneTwoStep::debug(); + + std::cout << SolutionOFOneTwoStep()(5, SolutionOFOneTwoStep::POLICY::RECURSIVE) << std::endl; + SolutionOFOneTwoStep::debug(); + + std::cout << SolutionOFOneTwoStep()(10, SolutionOFOneTwoStep::POLICY::NONRECURSIVE) << std::endl; + SolutionOFOneTwoStep::debug(); + + std::cout << SolutionOFOneTwoStep()(20, SolutionOFOneTwoStep::POLICY::RECURSIVE) << std::endl; + SolutionOFOneTwoStep::debug(); + return 0; } +