【试题描述】输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出序列。
【试题来源】未知
【参考代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include <iostream> #include <stack> using namespace std;
bool isPopOrder(const int* pushOrder, const int* popOrder, int length) {
if(pushOrder == NULL || popOrder == NULL || length <= 0) { return false; }
int i = 0; int j = 0; stack<int> tmpStack;
tmpStack.push(pushOrder[i++]);
while(i < length || j < length) {
if(tmpStack.top() != popOrder[j]) { tmpStack.push(pushOrder[i]); i++; } else { if(!tmpStack.empty()) { tmpStack.pop(); } else { return false; } j++; } }
if(tmpStack.empty()) { return true; } else { return false; } }
int main() { const int pushOrder[] = {1, 2, 3, 4, 5}; const int popOrder[] = {4, 3, 5, 1, 2}; int length = 5;
if(isPopOrder(pushOrder, popOrder, length)) { cout << "Yes" << endl; } else { cout << "No" << endl; }
return 0; }
|