Possible order of stack output

Source: Internet
Author: User

Tag: The first stack instead of the queue

Assume that the input sequence of the stack is 1234, and determine whether the following 4123,2431 is valid.
For 2431:

First let 1 enter the queue, then let 2 in the queue, let 2 pop up, Let 3 in, let 4 in, and finally pop up in sequence, the result is 2431

For 4123:

To let 4 out of the queue, we must first make 123 all in the queue. Then the order of 4 in the next line should be 3 rather than 1, so it is wrong.

Note:

1. The order of the team members must be kept unchanged.

2. You can also leave the team when you join the team.

3. Because it is a stack, it must be guaranteed to be an advanced post-release. Therefore, we generally first determine the approximate way to join the team based on the first pop-up number in the result to determine whether it is legal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.