Stack [] [] to prove safety Offer stack push, pop sequence

Title: two input sequence of integers, the first sequence indicating whether a sequence of stack of press-fitting, the second sequence is determined may make the pop-up sequence for the stack. All figures are not pushed onto the stack is assumed equal. Such as a sequence of a sequence of 1,2,3,4,5 is pressed into the stack, the push sequence is 4,5,3,2,1 sequence corresponding to a sequence of pop, but 4,3,5,1,2 it is impossible to push pop-up sequence of the sequence. (Note: the length of the two sequences are equal)

 

A: using an auxiliary stack, the first sequence numbers are sequentially pushed into the auxiliary stack, then the top element and the comparison loop stack sequences are equal

  If the last cycle, the auxiliary stack is empty, return true

  False otherwise

 

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

  

 

 

Related topics:

  Longest common sequence in brackets: https://www.nowcoder.com/practice/504ad6420b314e5bb614e1684ad46d4d

  Minimum stack: achieve a minimum stack, there are three operations, min: minimum value obtained in the stack, Push: inserting an element in the stack, POP: pop the top element, that the time complexity of these actions are O ( 1)  https://www.nowcoder.com/practice/a4d17eb2e9884359839f8ec559043761

  Get the maximum depth of n-dimensional array: input parameters for the n-dimensional array of type string, the value of each of the array of arrays or digital int. Please realize the maximum depth of a function, you can get a list of how many nested list.

 

Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11432695.html