Niuke.com's sword refers to Offer - the push and pop sequence of the stack

Topic description

Input two integer sequences. The first sequence represents the stack's push sequence. Please check whether the second sequence is the stack's pop-up sequence. Assume that all numbers pushed onto the stack are not equal. For example, the sequence 1, 2, 3, 4, 5 is the push sequence of a stack, the sequence 4, 5, 3, 2, 1 is a pop sequence corresponding to the stack sequence, but 4, 3, 5, 1, 2 It cannot be the pop sequence of the push sequence. (Note: the two sequences are of equal length)

Problem solving ideas

  • It is necessary to use an auxiliary stack, i and j are the subscripts of the current values ​​of pushV and popV, respectively, and then judge the values ​​that need to be pushed into the stack and the values ​​to be popped:
  • If the two are equal, it means that the value needs to be pushed in and then popped out, then i++ and j++ can be used;
  • If the two are not equal, judge whether popV[j] is equal to the top element in the current stack. If they are equal, pop the top element of the stack; if they are not equal, push pushV[i] into the stack;
  • If the value in pushV has been traversed, it is only necessary to judge whether popV[j] is equal to the top element in the current stack. If it is equal, the top element of the stack will be popped; if it is not equal, it means that the sequence is not the corresponding popup sequence, and return false ;
  • Until the value in popV is also traversed, indicating that the sequence is the corresponding popup sequence, return true.

code

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if( pushV.size() != popV.size() )
            return false;
        int i = 0;
        int j = 0;
        stack<int> s;
        while( 1 )
        {
            if( j == popV.size() )
                return true;
            if( i == pushV.size() )
            {
                if( popV[j] == s.top() )
                {
                    s.pop();
                    j++;
                }
                else
                    return false;
            }
            else if( pushV[i] == popV[j] )
            {
                i++;
                j++;
            }
            else if( !s.empty() && popV[j] == s.top() )
            {
                s.pop();
                j++;
            }
            else
            {
                s.push(pushV[i]);
                i++;
            }
        }
        return false;
    }
};

The sword refers to the idea of ​​​​offering

Similar to the above idea, but it is more organized and easier to understand. It is also necessary to establish an auxiliary stack. The specific steps are as follows:
(1) If the next popped number happens to be the top number on the stack, pop it directly;
(2) If the next popped number is not on the top of the stack, push the number that has not been pushed into the stack in the stack sequence into the auxiliary stack until the next number to be popped is pushed onto the top of the stack;

(3) If all numbers are pushed onto the stack and the next popped number is not found, then the sequence cannot be a popped sequence.

code

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        stack<int> st;
        int id=0;
        for(int i=0;i<popV.size();++i){
            while(st.empty()||st.top()!=popV[i]){
                st.push(pushV[id++]);
                if(id>pushV.size()){
                    return false;
                }
            }
            st.pop();
        }
        return st.empty();
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685127&siteId=291194637