【问题标题】:How to memoization using map?如何使用地图进行记忆?
【发布时间】:2013-06-29 08:57:22
【问题描述】:

这是我的代码。不行……谁能帮帮我?

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 );
}

我已经解决了这个问题。 Here's the Sample Solution!

【问题讨论】:

    标签: c++ map memoization


    【解决方案1】:

    您正在针对错误的容器检查 end()。据推测,resultsmap&lt;int,int&gt; 的另一个实例。

    results 更改为fibo

        if ( itr != fibo.end() )
    

    【讨论】:

    • 上传时间问题! :)
    • 你没有描述你认为错的地方。 It seems to work for me after this fix. 如果您要使用我的答案修复您的代码,然后说它仍然是错误的,那么您必须描述什么是错误的。
    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 2016-02-11
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2012-09-20
    相关资源
    最近更新 更多