From 4296e448ecd924aacaa55d862c1d41c5d88bb9cc Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Sat, 22 Feb 2025 00:09:39 +0800 Subject: [PATCH] feat: 70_climbing_stairs --- 70_climbing_stairs/main.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 70_climbing_stairs/main.cpp diff --git a/70_climbing_stairs/main.cpp b/70_climbing_stairs/main.cpp new file mode 100644 index 0000000..2c37483 --- /dev/null +++ b/70_climbing_stairs/main.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +class Solution { + public: + int fibonacci_1(int a, int b, int n) { + if (n <= 1) return b; + return fibonacci_1(b, a + b, n - 1); + } + + int fibonacci_2(int n) { + if (n == 2) return 2; + if (n == 1) return 1; + return fibonacci_2(n - 1) + fibonacci_2(n - 2); + } + + int climbStairs(int n) { + return fibonacci_1(1, 1, n); + } +};