给定两个长度相同的数组,找出哪些元素是 "shifted"

Given two arrays of the same length, find which elements are "shifted"

首先,这里有一些例子。

['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 小于两个数组的长度)。数组的长度始终相同。它们总是从数组的右侧移开。有办法吗?

我正在考虑在两个数组中找到最大的子集然后取右或其他东西。找不到处理它的好方法

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?

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

我正在使用 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']

这样的任务可以通过从末尾遍历第二个数组 (b) 并从它的末尾向后比较来实现。只需几行代码,就可以避免不必要的数组突变。可以通过向前跳过 ("accelerating" 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']