栈的压入和弹出
自己的想法:
class Solution {
public:
bool IsPopOrder(vector<int> pushV, vector<int> popV)
{
stack<int> temp;
temp.push(pushV[0]);
int len = pushV.size();
int biaozhi = 0;
for (int i = 0; i<len; ++i)
{
while (1)
{
if (temp.top() != popV[i])
{
++biaozhi;
if (biaozhi == len)
return false;
temp.push(pushV[biaozhi]);
}
else
{
temp.pop();
break;
}
}
}
return true;
}
};
bool IsPopOrder(vector<int> pushV, vector<int> popV)
{
stack<int> moni;
int index = 0;
int len = popV.size();
if (len == 0)
return false;
moni.push(pushV[0]);
for (int i = 0; i < len; ++i)//对于出栈队列的判断
{
while (moni.top() != popV[i])
{
//如果不相等继续进栈
++index;
if (index == len)
return false;
moni.push(pushV[index]);
}
moni.pop();
}
if (moni.empty())
return true;
else
return false;
}
结合栈和队列的方法:
int len=pushV.size();
queue qpop_V;
stack Stack;
for(int i=0;i<len;++i)
{
qpop_V.push(popV[i]);
}
for(int j=0;j<len;++j)
{
Stack.push(pushV[j]);
while(!Stack.empty()&&Stack.top()==qpop_V.front())
{
Stack.pop();
qpop_V.pop();
}
}
if(!Stack.empty())
return false;
else return true;