所有的字谜 - 递归

时间:2015-09-30 16:56:05

标签: javascript recursion

我试图理解下面的以下递归函数来教我自己的递归。我试图解释它低于预期的输出和功能本身。我错过了什么?我没有看到你从哪里来的过程' abc'到了' acb'。我试图理解它让我从'abc'权利' bac'。

template <class T> T* Singleton<T>::m_instance = nullptr;

我新的,正确的(我认为)解释:

    example usage:
        var anagrams = allAnagrams('abc');
        console.log(anagrams); // [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ]





        var allAnagrams = function(string) {

          var uniqueOutput = {};

          (function anagram(ana, str) {

            // could have also written this as: if(!str)....
            if (str === '') {
              uniqueOutput[ana] = 1;
            }
            //recursive call for the length of the anagram.
            for (var i = 0; i < str.length; i++) {
              anagram(ana + str[i], str.slice(0, i) + str.slice(i + 1));
              console.log(ana);
            }
          })('', string);
          console.log(uniqueOutput)
          return Object.keys(uniqueOutput);
        };


 // you are calling the recursive function like this: anagram([anagram=]'', [string=]'abc')
    // so the base case is not met on the first iteration since the string isn't empty
    // first iteration: i = 0
    // anagram('' + 'a' + 'bc' [from str.slice(0 +1)]) ---> resolves to "abc") ---> anagram("abc")
    //second iteration: i = 1. we are still in the original call from line 37.
    // here below, does "ana" stay as '' since we are still inside the initial recursion call?
    //anagram('' + b + a +  c) ---- resolves to "bac" ---> anagram("bac")
    //third and last iteration iteration: i = 2
    //anagram(" " + c + c) ----> anagram("cc") ? not a valid result.

2 个答案:

答案 0 :(得分:3)

根据您的评论:

// first iteration: i = 0
// anagram('' + 'a' + 'bc' [from str.slice(0 +1)]) ---> resolves to "abc") --->

实际上在i = 0上,传递给anagram的参数是:

anagram('' + 'a', '' + 'bc');

评估为:

anagram('a', 'bc');

然后在对anagram的调用中,我们再次循环str,现在只是'bc'。这将导致另外两次调用anagram,这将是

anagram('a' + 'b', '' + 'c'); // i = 0
anagram('a' + 'c', 'b' + ''); // i = 1

评估为:

anagram('ab', 'c');
anagram('ac', 'b');

这些调用中的第二个将导致使用这些参数再次调用anagram

anagram('acb', ''); 

uniqueOutput添加到str现在为空。

只有在执行了所有操作后,代码才会返回anagram的最外层调用,而i将根据您的评论增加:

//second iteration: i = 1. we are still in the original call from line 37.

答案 1 :(得分:2)

您缺少第二次迭代..这是评论中的执行流程:

    // anagram('', 'abc') level 0
      // condition false... level 0
      // first iteration: i = 0  level 0
      // anagram(a', 'bc') ( creating new execution context level 1 )
        //  condition false.. level 1
        //  iteration 1... level 1
        // anagram('ab', 'c') ( creating new execution context level 2 )
          //  condition false..  level 2
          //  iteration 1... level 2
          // anagram('abc', '') ( creating new execution context level 3 )
            // condition true.. push "abc"
          // end for of level 2 context execution
        // iteration 2...  level 1
        // anagram('ac', 'b') ( creating new execution context level 2 )
          //  condition false.. level 2
          //  iteration 1... level 2
          // anagram('acb', '') ( creating new execution context level 3 )
            // condition true.. push "abc" level 3
          // end for of level 2 execution
        // end for of level 1
      // second iteration of level 0....
      // keep this pattern till end for of level 0..
    // end anagram level 0

正如您所看到的,0级的每次迭代都会将2个单词推送到对象。如果我们遵循你的逻辑,每次迭代只推一个,考虑到递归就像在那个确切的位置添加更多代码一样,一旦函数调用完成,执行流程返回到函数调用之前的位置