Skip to content

Commit 547e13d

Browse files
committed
Time: 0 ms (100.00%), Space: 6 MB (18.24%) - LeetHub
1 parent 6ea844e commit 547e13d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
class Solution {
22
public:
33
int fib(int n) {
4-
if(n <= 1) return n;
5-
return fib(n-1) + fib(n-2);
4+
vector<int> F(50);
5+
F[0] = 0;
6+
F[1] = 1;
7+
if(n == 0)
8+
return 0;
9+
if(n == 1)
10+
return 1;
11+
for(int i = 2; i<=n; i++)
12+
{
13+
F[i] = F[i-1] + F[i-2];
14+
}
15+
return F[n];
616
}
717
};

0 commit comments

Comments
 (0)