题目描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
#include<stack>
#include<vector>
using namespace std;
class Solution {
public:
bool IsPopOrder(vector<int> pushV, vector<int> popV) {
if (pushV.empty() || pushV.size() != popV.size())return false;
int left = 0, index = 0;
stack<int> stk;
while (1)
{
if ((!stk.empty()) && stk.top() == popV[index]){
index++;
stk.pop();
}
else{
bool nofind = 1;
for (int i = left; i <= pushV.size() - 1; i++){
if (popV[index] == pushV[i]){ index++; left = i + 1; nofind = 0; break; }
stk.push(pushV[i]);
}
if (nofind)return false;
}
if (index == popV.size())return true;
}
return true;
}
};
class Solution1 {
public:
bool IsPopOrder(vector<int> pushV, vector<int> popV) {
if (pushV.empty() || pushV.size() != popV.size())return false;
int left = 0, index=0;
stack<int> stk;
while (1)
{
if ((!stk.empty())&&stk.top() == popV[index]){
index++;
stk.pop();
}
else{
bool nofind = 1;
for (int i = left; i <= pushV.size() - 1; i++){
if (popV[index] == pushV[i]){ index++; left = i + 1; nofind = 0; break; }
stk.push(pushV[i]);
}
if (nofind)return false;
}
if (index == popV.size())return true;
}
return true;
}
};
int main()
{
int v1[] = { 1,2,3,4,5};
int v2[] = { 4, 3, 5,1,2};
vector<int> vpush(&v1[0], &v1[sizeof(v1) / sizeof(int)]), vpop(&v2[0], &v2[sizeof(v2) / sizeof(int)]);
Solution mys;
cout << mys.IsPopOrder(vpush, vpop) << endl;
cout << "end" << endl;
system("pause");
}