牛客剑指offer,栈的压入弹出
程序员文章站
2022-07-14 20:22:25
...
class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
bool bPossible =false;
int nLength = pushV.size();
if(!pushV.empty() && !popV.empty() && nLength > 0)
{
int i = 0;
int j = 0;
stack<int> s1;
while(j< nLength-1)
{
while(s1.empty()||s1.top()!=popV[j])
{
if(i== nLength-1)
{
break;
}
s1.push(pushV[i]);
i++;
}
if(s1.top()!= popV[j])
break;
s1.pop();
j++;
}
if(s1.empty() && j == nLength-1)
bPossible = true;
}
return bPossible;
}
};
下一篇: 剑指 offer -- 刷题记录