简体   繁体   中英

How to memoization using map?

Here is my code. It's not work...Anyone can help me?

map<int,int> fibo;

int fibonacci( int n )
{
    if ( n == 0 || n == 1 )
        return 1;
    map<int,int>::iterator itr = fibo.find( n );
    if ( itr != fibo.end() )
        return itr->second;
    else
        return fibo[ n ] = fibonacci( n -1 ) + fibonacci( n - 2 );
}

i've solved this. Here's the Sample Solution!

You are checking for end() against the wrong container. Presumably, results is another instance of a map<int,int> .

Change results to fibo :

    if ( itr != fibo.end() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM