Skip to content

Instantly share code, notes, and snippets.

@crimx
Created August 26, 2014 14:29
Show Gist options
  • Save crimx/9cd37be69746d27cc625 to your computer and use it in GitHub Desktop.
Save crimx/9cd37be69746d27cc625 to your computer and use it in GitHub Desktop.
有2个int型已经去重的数组a和b,都是已经从小到大排序好的。要求遍历b数组的数字,如果这个数字出现在a中,就将其从a删去;反之将其插入到a的适当位置,使a保持排序状态。
// O(m+n)
var foo = function(arrA, arrB) {
var iB,
iA;
for (iA = iB = 0; iB < arrB.length; iB += 1) {
while (arrB[iB] > arrA[iA] && iA < arrA.length) {
iA += 1;
}
if (arrB[iB] === arrA[iA]) {
arrA.splice(iA, 1);
} else {
arrA.splice(iA+1, 0, arrB[iB]);
}
}
return arrA;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment