栈的压入、弹出序列

题目描述

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。
class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        stack<int> s;
        int len1=pushV.size();
        int len2=popV.size();
        if(len1!=len2)
              return false;
        if(len1==0||len2==0)
               return false;
        int i,j;
         j=0;
        s.push(pushV[0]);
        i=1;
         while(i<=len1){
            while(s.top()==popV[j]){
                 j++;
                s.pop();
                 if(j==len2)
                {
                   break;
                }
            }
           s.push(pushV[i]);
                i++;
          }
        if(j==len2)
             return true;
        else
             return false;
    
    }
};

 

posted @ 2015-11-03 20:52  疯狂的癫子  阅读(118)  评论(0编辑  收藏  举报