-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci_number.cpp
More file actions
80 lines (71 loc) · 1.54 KB
/
fibonacci_number.cpp
File metadata and controls
80 lines (71 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 509. Fibonacci Number: https://leetcode.com/problems/fibonacci-number
// Author: xianfeng.zhu@gmail.com
#include <initializer_list>
#include <unordered_map>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::initializer_list;
using std::pair;
using std::vector;
// Recursive
class Solution1 {
public:
int fib(int n) {
std::unordered_map<int, int> cache;
cache[0] = 0;
cache[1] = 1;
return fib(n, cache);
}
private:
int fib(int n, std::unordered_map<int, int>& cache) {
auto iter = cache.find(n);
if (iter != cache.end()) {
return iter->second;
}
int val = fib(n - 2, cache) + fib(n - 1, cache);
cache[n] = val;
return val;
}
};
// Dynamic programming
class Solution2 {
public:
int fib(int n) {
if (n < 1) {
return 0;
}
vector<int> dp(n + 1, 0);
dp[1] = 1;
for (int i = 2; i < n + 1; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
};
// Dynamic programming with less space
class Solution3 {
public:
int fib(int n) {
int a = 0, b = 1, c = 0;
for (int i = 2; i < n + 1; i++) {
c = a + b;
a = b;
b = c;
}
return (n > 1 ? c : n);
}
};
TEST(Solution, fib) {
initializer_list<pair<int, int>> cases = {
std::make_pair(2, 1),
std::make_pair(3, 2),
std::make_pair(4, 3),
};
for (auto c : cases) {
EXPECT_EQ(Solution1().fib(c.first), c.second);
EXPECT_EQ(Solution2().fib(c.first), c.second);
EXPECT_EQ(Solution3().fib(c.first), c.second);
}
}