Possible stack sequence problems

Source: Internet
Author: User
Possible stack sequence problems

The first premise is that the stack must be in accordance with the order into the stack, such as 1, 2, 3, 4 in the order, if the first out of 4, then the advanced stack in sequence 1, 2, 3, 4,
Then the stack, so that the first is 4, no other elements can be in the stack, so only in order out of the stack, so the order of the stack is 4, 3, 2, 1.
If the order of the stack is 3, 4, 2, 1, you must first analyze the situation of 3, only the first 1, 2, 3 into the stack, and then 3 out of the stack. And then into the stack 4, and then out of the stack 4, 2, 1.

If the order of the stack is 3, 2, 4, 1 or 3, 2, 1, 4, first the 1, 2, 3 into the stack, again out of the stack 3, 2, if this is the front of the sequence, the Advanced 4, and then out of the stack 4, 1, if it is the back of the stack order out of 1, and then into the stack 4, and then out of the stack 4, so you will get the above sequence.

can be judged according to the following method: If the stack order is 1234, given a stack sequence, such as 2431, it is legal.
Because for each number in the stack sequence, all numbers behind it, which are smaller than it, must be in descending order.


For example: if the stack sequence is 4123, obviously does not meet the above requirements, because for 4, it later than its small number sequence of 123, accumulator, so is not a legitimate stack sequence.
If the stack is 3142, it is also illegal, because 3 is smaller than the 1 and 2 is not descending order.
If the stack is 1234, then it is legal, because for each number, there is no smaller number behind it.
This method is used to rule out the wrong stack order.


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.