题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
int i=0,j=0;//一个指向pushV数组,一个指向popV数组
stack<int >S;
while(1){
if(i==pushV.size()&&S.top()!=popV[j]){//此时直接返回false
return false;
}
while(i<pushV.size()&&(S.empty()||S.top()!=popV[j])){//当栈为空或者栈顶元素不等于popV[j]时入栈
S.push(pushV[i++]);
}
while(!S.empty()&&S.top()==popV[j]){
S.pop();
j++;
}
if(i==pushV.size()&&S.empty())
return true;
}
return true;
}
};
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
int i=0,j=0;//一个指向pushV数组,一个指向popV数组
stack<int >S;
while(1){
if(i==pushV.size()&&S.top()!=popV[j]){//此时直接返回false
return false;
}
while(i<pushV.size()&&(S.empty()||S.top()!=popV[j])){//当栈为空或者栈顶元素不等于popV[j]时入栈
S.push(pushV[i++]);
}
while(!S.empty()&&S.top()==popV[j]){
S.pop();
j++;
}
if(i==pushV.size()&&S.empty())
return true;
}
return true;
}
};