1 var ins = [1, 2, 3, 4, 5]; 2 var outs = [3, 4, 5, 2, 1]; 3 var temp = ins; 4 var num, i = 0, len; 5 6 /** 7 * ins 入栈顺序 8 * outs 出栈顺序 9 * temp 临时数组,保存入栈顺序 10 * num 是临时数组中 当前出栈节点的下标 11 * len 临时数组的长度-1 12 * 13 */ 14 (function (temp, outs) { 15 if(ins == null || temp == null){ 16 console.log("不能为空"); 17 } 18 for (i = 0; i < outs.length; i++) { 19 num = temp.indexOf(outs[i]); 20 len = temp.length - 1; 21 if (num > 0 || num < len) { 22 if ((outs[i + 1] == temp[num - 1]) || (outs[i + 1] == temp[num + 1])) { 23 temp.splice(num, 1); 24 continue; 25 } 26 console.log('不是正确的出栈顺序'); 27 return false; 28 } else if (num == 0) { 29 if (outs[i + 1] == temp[num + 1]) { 30 temp.splice(num, 1); 31 continue; 32 } 33 console.log('不是正确的出栈顺序'); 34 return false; 35 } else if (num == len) { 36 if (outs[i + 1] == tem[num - 1]) { 37 temp.splice(num, 1); 38 continue; 39 } 40 console.log('不是正确的出栈顺序'); 41 return false; 42 } else { 43 console.log('不是正确的出栈顺序'); 44 return false; 45 } 46 } 47 console.log("正确"); 48 })(temp, outs);