置换树对代码没有意义

问题描述 投票:0回答:1

[我观看了一个YouTube视频,其中显示了this基本排列树。如果您看一下这段代码:

    function recursion(input, set = [], result = []) {
        if (!input.length) {
           result.push([...set].join(''));
         }

        for (let i = 0; i < input.length; i++) {
            const newArr = input.filter((n, index) => index !== i);
            set.push(input[i]);
            recursion(newArr, set, result);
            set.pop();
         }
         return result.join(', ');
}

您可以看到基本情况(if语句)在过滤参数nums之前位于顶部。因此,我的整个问题是树和代码如何有意义,因为对我而言,代码将从set数组中删除一位数字。因为它在返回时弹出一个项目,并且返回不超过两次吗?

javascript recursion permutation
1个回答
0
投票

此日志是否增加清晰度?

/ entering recursion with input = [1,2,3], set = [], result = []
| looping, i = 0
| adding  1 to set
|   / entering recursion with input = [2,3], set = [1], result = []
|   | looping, i = 0
|   | adding  2 to set
|   |   / entering recursion with input = [3], set = [1,2], result = []
|   |   | looping, i = 0
|   |   | adding  3 to set
|   |   |   / entering recursion with input = [], set = [1,2,3], result = []
|   |   |   | adding 123 to result
|   |   |   \ returning [123]
|   |   | removing  3 from set
|   |   \ returning [123]
|   | removing  2 from set
|   | looping, i = 1
|   | adding  3 to set
|   |   / entering recursion with input = [2], set = [1,3], result = [123]
|   |   | looping, i = 0
|   |   | adding  2 to set
|   |   |   / entering recursion with input = [], set = [1,3,2], result = [123]
|   |   |   | adding 132 to result
|   |   |   \ returning [123,132]
|   |   | removing  2 from set
|   |   \ returning [123,132]
|   | removing  3 from set
|   \ returning [123,132]
| removing  1 from set
| looping, i = 1
| adding  2 to set
|   / entering recursion with input = [1,3], set = [2], result = [123,132]
|   | looping, i = 0
|   | adding  1 to set
|   |   / entering recursion with input = [3], set = [2,1], result = [123,132]
|   |   | looping, i = 0
|   |   | adding  3 to set
|   |   |   / entering recursion with input = [], set = [2,1,3], result = [123,132]
|   |   |   | adding 213 to result
|   |   |   \ returning [123,132,213]
|   |   | removing  3 from set
|   |   \ returning [123,132,213]
|   | removing  1 from set
|   | looping, i = 1
|   | adding  3 to set
|   |   / entering recursion with input = [1], set = [2,3], result = [123,132,213]
|   |   | looping, i = 0
|   |   | adding  1 to set
|   |   |   / entering recursion with input = [], set = [2,3,1], result = [123,132,213]
|   |   |   | adding 231 to result
|   |   |   \ returning [123,132,213,231]
|   |   | removing  1 from set
|   |   \ returning [123,132,213,231]
|   | removing  3 from set
|   \ returning [123,132,213,231]
| removing  2 from set
| looping, i = 2
| adding  3 to set
|   / entering recursion with input = [1,2], set = [3], result = [123,132,213,231]
|   | looping, i = 0
|   | adding  1 to set
|   |   / entering recursion with input = [2], set = [3,1], result = [123,132,213,231]
|   |   | looping, i = 0
|   |   | adding  2 to set
|   |   |   / entering recursion with input = [], set = [3,1,2], result = [123,132,213,231]
|   |   |   | adding 312 to result
|   |   |   \ returning [123,132,213,231,312]
|   |   | removing  2 from set
|   |   \ returning [123,132,213,231,312]
|   | removing  1 from set
|   | looping, i = 1
|   | adding  2 to set
|   |   / entering recursion with input = [1], set = [3,2], result = [123,132,213,231,312]
|   |   | looping, i = 0
|   |   | adding  1 to set
|   |   |   / entering recursion with input = [], set = [3,2,1], result = [123,132,213,231,312]
|   |   |   | adding 321 to result
|   |   |   \ returning [123,132,213,231,312,321]
|   |   | removing  1 from set
|   |   \ returning [123,132,213,231,312,321]
|   | removing  2 from set
|   \ returning [123,132,213,231,312,321]
| removing  3 from set
\ returning [123,132,213,231,312,321]

您可以在以下代码段中看到如何将日志记录添加到您的代码中:

const log = (depth, message) => 
  console .log ('|   '.repeat (depth)  + message)


function recursion(input, set = [], result = [], depth = 0) {
    log (depth, `/ entering recursion with input = [${input}], set = [${set}], result = [${result}]`)
    if (!input.length) {
        log (depth, `| adding ${[...set].join('')} to result`)
        result.push([...set].join(''));
     }

    for (let i = 0; i < input.length; i++) {
        log (depth, `| looping, i = ${i}`)
        const newArr = input.filter((n, index) => index !== i);
        log (depth, `| adding  ${input[i]} to set` )
        set.push(input[i]);
        recursion(newArr, set, result, depth + 1);
        log (depth, `| removing  ${input[i]} from set` )
        set.pop();
    }
    log (depth, `\\ returning [${result}]`)
    return result.join(', ');
}

console .log (recursion([1, 2, 3]))
.as-console-wrapper {min-height: 100% !important; top: 0}

(但控制台输出限制为最后50行。)>

© www.soinside.com 2019 - 2024. All rights reserved.