題目描述
輸入兩個(gè)整數(shù)序列,第一個(gè)序列表示棧的壓入順序疗锐,請(qǐng)判斷第二個(gè)序列是否為該棧的彈出順序愉豺。假設(shè)壓入棧的所有數(shù)字均不相等。例如序列1,2,3,4,5是某棧的壓入順序矮嫉,序列4削咆,5,3,2,1是該壓棧序列對(duì)應(yīng)的一個(gè)彈出序列,但4,3,5,1,2就不可能是該壓棧序列的彈出序列蠢笋。(注意:這兩個(gè)序列的長(zhǎng)度是相等的)
class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
stack<int> stackV;
int pushIndex = 0;
int popIndex = 0;
while(pushV[pushIndex] != popV[popIndex]) {
stackV.push(pushV[pushIndex++]);
}
++popIndex;
++pushIndex;
while(popIndex < popV.size()) {
if(popV[popIndex] == stackV.top()) {
stackV.pop();
++popIndex;
}
else{
stackV.push(pushV[pushIndex++]);
}
//++popIndex;
}
if(stackV.empty())
return true;
return false;
}
};