/**
* 一副扑克牌,一张放桌上一张放牌底,
* 直到所有牌都在桌上,告诉你桌上牌的顺序,求原顺序
*
* 解法:
* 原来的顺序可以理解为:
* 设桌面牌数组 C,卡牌双向栈S
* 1. C.push(S.pop());
* 2. S.push_back(S.pop());
* 这两步的逆运算:
* 1. S.push(S.back());
* 2. S.push(C.back());
*/
const cards = ['A', 'C', 'B', 'D'];
function getOriginOrder(nowOrder) {
let origin = [];
let pt = nowOrder.length - 1; // 原数组指针
while (pt >= 0) {
if (origin.length > 0) {
const back = origin[origin.length-1];
origin.splice(origin.length-1, 1);
origin = [back, ...origin];
}
const c_back = nowOrder[pt];
origin = [c_back, ...origin];
pt--;
}
return origin;
}
console.log(getOriginOrder(cards));
console