简体   繁体   English

我对 function 的工作原理有疑问

[英]I have a question about how the function works

While solving the algorithm, there is a part that I do not understand about how the function works.在解决算法时,有一部分我不明白 function 的工作原理。

function letterCombinations(digits) {
    var map = ['0', '1', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
    var res = [];
    var prefix = [];

    if (digits.length) {
        traverse(0);
    }
    return res;

    function traverse(idx) {
        if (idx === digits.length) {
            return res.push(prefix.join(''));
        }

        var str = map[digits[idx] - '0'];

        for (var i = 0; i < str.length; i++) {
            prefix.push(str[i]);
            traverse(idx + 1);
            prefix.pop();
        }
    }
}

When the conditional statement inside the traverse function is encountered当遇到遍历function里面的条件语句时

 if (idx === digits.length) {
        return res.push(prefix.join(''));
    }

Why is the pop method executed without exiting the function?为什么在没有退出function的情况下执行pop方法?

prefix.pop()

You wording is kinda ambiguous, but I think get what you're really asking.你的措辞有点模棱两可,但我想明白你真正要问的。

The prefix.pop() is a line inside traverse() function, and the traverse() recursively calls itself. prefix.pop()traverse() function 里面的一行, traverse()递归调用自己。

Take for example letterCombinations('23') where corresponding str would be 2: 'abc', 3: 'def' .letterCombinations('23')为例,其中对应的str2: 'abc', 3: 'def' The process can be visualized as following pseudo code:该过程可以可视化为以下伪代码:

traverse(0)
  i=0, push('a') // prefix=['a']

  traverse(1)
    loop when i=0:
      push('d')       // prefix=['a', 'd']
      traverse(2)     // since `2==digits.length`, it's a short-circuit return
                      // no `pop()` called inside `traverse(2)`
      pop_1() -> 'd'  // prefix=['a']
    
    loop when i=1:
      push('e')       // prefix=['a', 'e']
      traverse(2)     // short-circuit return
      pop_1() -> 'e'  // prefix=['a']

    loop when i=3:
      ...
    
  pop_0()

You get confused because you're fixing your eye on the prefix.pop() line of code in traverse(0) , marked as pop_0() in the pseudo code.您会感到困惑,因为您正在关注traverse(0)中的prefix.pop()代码行,在伪代码中标记为pop_0() But you forget to trace the recursion.但是您忘记了跟踪递归。

Use your imagination and step into traverse(1) , you'll find another prefix.pop() call there, marked as pop_1() .发挥您的想象力并踏入traverse(1) ,您会在那里找到另一个prefix.pop()调用,标记为pop_1()

So to answer your question:所以回答你的问题:

Why is the pop method executed without exiting the function?为什么在没有退出function的情况下执行pop方法?

You're right about the fact that, we haven't exit traverse(0) yet, but the pop method is actually called inside traverse(1) .你说得对,我们还没有退出traverse(0) ,但是 pop 方法实际上是在traverse(1)内部调用的。 Out there, we've exit traverse(2) because of the short-circuit return.在那里,由于短路返回,我们退出了traverse(2)

The .pop() method has nothing to do with the function it's being called in. That method is part of the array object, see docs .pop()方法与它被调用的 function 无关。该方法是数组 object 的一部分, 请参阅文档

The pop() method removes the last element from an array and returns that element. pop() 方法从数组中删除最后一个元素并返回该元素。 This method changes the length of the array.此方法更改数组的长度。

The return statement actually exists the function. return语句实际上存在 function。 So if you want when .pop() is called, the function to be exited write:因此,如果您想在调用.pop()时退出 function,请写入:

return prefix.pop();

Or if you want the current iteration of the loop finished, you can call continue或者,如果您希望循环的当前迭代完成,您可以调用continue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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