memoization fibonacci c 2b 2b

Solutions on MaxInterview for memoization fibonacci c 2b 2b by the best coders in the world

showing results for - "memoization fibonacci c 2b 2b"
Ashley
30 May 2017
1#include <map>
2
3int fibonacci(int n)
4{
5  static std::map<int, int> values;
6  if (n==0 || n==1)
7    return n;
8  std::map<int,int>::iterator iter = values.find(n);
9  if (iter == values.end())
10  {
11    return values[n] = fibonacci(n-1) + fibonacci(n-2);
12  }
13  else
14  {
15    return iter->second;
16  }
17}
18
Hanna
30 Oct 2017
1#include <map>
2
3int fibonacci(int n, std::map<int, int> values)
4{
5  if (n==0 || n==1)
6    return n;
7  std::map<int,int>::iterator iter = values.find(n);
8  if (iter == values.end())
9  {
10    return values[n] = fibonacci(n-1) + fibonacci(n-2);
11  }
12  else
13  {
14    return iter->second;
15  }
16}
17