aboutsummaryrefslogtreecommitdiff
path: root/other/fibonacci_number.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/fibonacci_number.cpp')
-rw-r--r--other/fibonacci_number.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/other/fibonacci_number.cpp b/other/fibonacci_number.cpp
new file mode 100644
index 0000000..69e46ee
--- /dev/null
+++ b/other/fibonacci_number.cpp
@@ -0,0 +1,20 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int fib(int n) {
+ if (n < 2)
+ return n;
+ int ans[n + 1];
+ ans[0] = 0;
+ ans[1] = 1;
+ for (int i = 2; i <= n; i++)
+ ans[i] = ans[i - 1] + ans[i - 2];
+ return ans[n];
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.fib(2);
+}