给定两个相同长度的数组,找出哪些元素被移位"

时间:2015-10-02 19:31:21

标签: javascript arrays underscore.js lodash

首先,这里有一些例子。

['d', 'b', 'c', 'd', 'e']
['c', 'd', 'e', 1, 2]
// desired result: [1, 2]

[1, 2, 3]
[2, 3, 4]
// desired result: [4]

['Hello', 'Goodbye', 'Goodbye', 'Hello']
['Goodbye', 'Hello', 'Goodbye', 'Goodbye']
// desired result: ['Goodbye', 'Goodbye']

元素在n个索引中移位(其中n小于两个数组的长度)。数组的长度始终相同。它们总是从阵列的右侧移位。有没有办法做到这一点?

我正在考虑找到两个阵列中最大的子集,然后选择正确的东西。无法找到接近它的好方法

3 个答案:

答案 0 :(得分:3)

逻辑有点简单,主要问题是比较两个数组。

我正在使用JSON.stringify来执行此操作,但也可以遍历数组。

首先你循环进入arr a并移动它,直到它与b的第一项具有相同的元素。

然后,你从arr b的长度得到arr a和切片。

function findCrazyShifts(a, b) {
  while (JSON.stringify(a) !== JSON.stringify(b.slice(0, a.length))) {
    a.shift();
  }

  return b.slice(a.length);
}

console.log(findCrazyShifts(['d', 'b', 'c', 'd', 'e'], ['c', 'd', 'e', 1, 2]));
// result: [1, 2]

console.log(findCrazyShifts([1, 2, 3], [2, 3, 4]));
// result: [4]

console.log(findCrazyShifts(['Hello', 'Goodbye', 'Goodbye', 'Hello'], ['Goodbye', 'Hello', 'Goodbye', 'Goodbye']));
// result: ['Goodbye', 'Goodbye']

答案 1 :(得分:2)

function unshifter(a,b) {  
  while(!a.equals(b.slice(0,a.length))) {
    a.shift();
  }
  return b.slice(a.length, b.length);
}

// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;

    // compare lengths - can save a lot of time 
    if (this.length != array.length)
        return false;

    for (var i = 0, l=this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].equals(array[i]))
                return false;       
        }           
        else if (this[i] != array[i]) { 
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;   
        }           
    }       
    return true;
}  

console.log(unshifter(['d', 'b', 'c', 'd', 'e'], ['c', 'd', 'e', 1, 2]));
console.log(unshifter([1, 2, 3], [2, 3, 4]));
console.log(unshifter(['Hello', 'Goodbye', 'Goodbye', 'Hello'], ['Goodbye', 'Hello', 'Goodbye', 'Goodbye']));

此代码使用How to compare arrays in JavaScript?

答案 2 :(得分:1)

这样的任务可以通过从末尾迭代第二个数组( b )并向后比较从它的结尾来实现。只需几行代码,这就避免了不必要的数组变异。可以通过跳过前进(“加速” i )取决于内部比较中发现的内容,以代码复杂性为代价进一步优化,但我把这一点留下来,因为这里似乎没有必要:

function getShifted(a, b) {
  if(a.length != b.length)
    return;

  var len = a.length;
  for(var i = b.length - 1, j; i >= 0; i--) {
    for(j = 0; j <= i && a[len - j - 1] == b[i - j]; j++);
    if(j > i) {
      return b.slice(i + 1);
    }
  }

  return [];
}


function writeShifted(a, b) {
  document.write('a: ' + JSON.stringify(a));
  document.write('<br />');
  document.write('b: ' + JSON.stringify(b));
  document.write('<br />');
  document.write('shifted: ' + JSON.stringify(getShifted(a, b)));
  document.write('<br /><br />');
}

writeShifted(['d', 'b', 'c', 'd', 'e'],
             ['c', 'd', 'e', 1, 2]);
// desired result: [1, 2]

writeShifted([1, 2, 3],
             [2, 3, 4]);
// desired result: [4]

writeShifted(['Hello', 'Goodbye', 'Goodbye', 'Hello'],
             ['Goodbye', 'Hello', 'Goodbye', 'Goodbye']);
// desired result: ['Goodbye', 'Goodbye']