# 爱莲学堂

## 一道腾讯前端试题，谁来试试身手

var ary = [1, 5, 7, 6, 4, 8, 10];
var n = ary.length + 3;
var newAry = [];
document.write("假设n=" + n + "<br/>");

ary.sort(function(a, b){
return a - b;
});
document.write("初始数组:" + ary + "<br/>");
for(var i = 1, j=0; i <= n; i ++,j++){
var diff = ary[j] - i;
if(!ary[j]){
newAry.push(i);
} else if(diff > 0){
for(var k = 0; k < diff; k ++){
newAry.push(i++);
}
}
}

document.writeln("缺少的数:" + newAry);

var  n = 10;
var oldArr = [5,1,6,3,7,8,10];//缺失的源数组997个数；
var newArr = Array(11);
var lostArr = [];//要找的数的数组
for(var i = 0; i < n-3; i++) {
newArr[oldArr[i]] = 1;
}
for(var j = 0; j < newArr.length; j++) {
if(!newArr[j]) {
lostArr.push(j);
}
}
lostArr.shift(0);
alert(lostArr);

var num = [2,1,3,5,4,6,7,9,10,11,12,14,15,17,18,19,20,22,23,21];
num = num.sort(function(a,b){return a-b});
var y={};
for(var i=0;i<num.length;i++){
y[num[i]] = num[i];
}

var m=1;
var k=[];
while(m<=23){
if(!(m in y)){
k.push(m);
}
m++;
}
alert(k);

posted on 2012-07-01 07:38 爱莲学堂 阅读(...) 评论(...) 编辑 收藏