简体   繁体   English

Javascript计算排列-当我不复制数组时,为什么我的代码返回不需要的解决方案?

[英]Javascript computing permutations - why is my code returning unwanted solution when I don't make a copy of the array?

Below is code for a function that returns all permutations of a given array. 以下是返回给定数组的所有排列的函数的代码。

function getAllPerms(s) {
  var perms = [];
  if (s.length === 1) {
    perms.push(s);
    } else {
        for (var i = 0; i < s.length; i++) {
            var sub = s.slice(0);
            sub.splice(i, 1);
            var sp = getAllPerms(sub);
            for (var o = 0; o < sp.length; o++) {
                sp[o].unshift(s[i]);
                perms.push(sp[o]);
            }
        }
    }
    return perms;
  }
  console.log(getAllPerms([1,2]));  // result is [[1, 2], [2, 1]]

however if i don't make a copy of the array and splice the original array I don't get the same output, and I have been racking my brain to understand why? 但是,如果我不复制该数组并拼接原始数组,则不会得到相同的输出,并且我一直在绞尽脑汁理解为什么? To me, it seems as though it should work either way. 对我来说,似乎它应该以任何一种方式起作用。 Below is the code with changes to the original array. 以下是更改原始数组的代码。

function getAllPerms(s) {
    var perms = [];
    var len = s.length
    if (s.length === 1) {
        perms.push(s);
    } else {
        for (var i = 0; i < len; i++) {
            var digit = s[i];
            s.splice(i, 1);
            var sp = getAllPerms(s);
            for (var o = 0; o < sp.length; o++) {
                sp[o].unshift(digit);
                perms.push(sp[o]);
            }
        }
    }
    return perms;
}
console.log(getAllPerms([1,2])); // result is [[2, 1], [2, 1]]

For the first set of code I get the correct result,[[1, 2], [2, 1]], but for the second bit of code I get something strange, [[2, 1], [2, 1]]. 对于第一组代码,我得到正确的结果,[[1,2],[2,1]],但是对于第二部分代码,我得到了奇怪的结果,[[2,1],[2,1] ]。 Cannot for the life of me figure out what is going on. 无法为我的生活弄清楚发生了什么事。

When you run the code that changes the original array, you're actually modifying the input array s to be first [1,2] then [2,1] , and your return array is just the original array s multiple times, so modifying s to become the second element also modifies it as the first element. 当您运行更改原始数组的代码时,实际上是将输入数组s修改为首先[1,2]然后是[2,1] ,而返回数组只是原始数组s多次,因此修改成为第二个元素的s也会将其修改为第一个元素。

You can see the effect more simply by running the following: 通过运行以下命令,您可以更简单地看到效果:

var x = [1,2]
var p = []
p.push(x)
x[0] = 2
x[1] = 1
p.push(x)

Observe how x and p change after each line. 观察每行之后xp变化。

暂无
暂无

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

相关问题 (JavaScript,Redux)为什么我的嵌套数组在返回时被删除,但是当我不返回时却没有? - (JavaScript, Redux) Why are my nested array's getting deleted when returning, however when I don't return it doesn't? 为什么我的 javascript 不能重定向以下代码,我尝试了多种解决方案,我在 StackOverFlow 中找到了 - Why can't my javascript redirect the following code, I've tried multiple solution that i found in StackOverFlow 从排序数组中删除重复项 - 我不明白为什么我的解决方案在某些情况下不起作用 - Remove Duplicates from Sorted Array - I don't understand why my solution doesn't work in certain situation 我不知道为什么我的JavaScript函数Flip()不返回任何东西。 我希望它返回是正面还是反面 - I don't know why my function Flip() in JavaScript isn't returning anything. I want it to return if it is heads or tails 我不明白,为什么我的 CalculateTotal () function 返回“Null”? - I don't understand, Why is my CalculateTotal () function returning "Null"? 为什么在用ajax重新加载部分页面后,此javascript代码不起作用? - why this javascript code don't work after i reload part of my page with ajax? 我的代码刚刚决定停止工作,我不知道为什么。 (javascript 谷歌 appscript) - My code just decided to stop working and I don't know why. (javascript google appscript) 我不了解计算/ javascript的字符串到字节方面 - I don't understand the string to byte aspect of computing/javascript 为什么数组的相应部分在我的代码中不匹配? - Why don't the corresponding parts of the array match up in my code? 当我在 Javascript 中调用它时,样式不适用于我的代码的一部分? - The Style don't apply in a part of my code when I call it in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM