Use the stack to check whether the order of a sequence is legal

For example: The existing sequence sequence is 32541, and the stack sequence is 12345. The algorithm for checking whether 32541 is legal
is as follows:

bool check_is_valid_order(std::queue<int>& order) {
    
    
    stack<int> S;
    int n = order.size();
    for (int i = 0; i <= n; i++)
    {
    
    
        S.push(i);
        while (!S.empty()&&order.front()==S.top())
        {
    
    
            S.pop();
            order.pop();
        }
    }
    if (!S.empty())
    {
    
    
        return false;
    }
    return true;
}

ps: Notes for the Little Elephant Academy Tutorial https://www.bilibili.com/video/BV1GW411Q77S?t=7029&p=2

Guess you like

Origin blog.csdn.net/weixin_44427114/article/details/107895419