Write an algorithm to determine if the stack order is correct

Source: Internet
Author: User

Data into the stack: 1234567

Solution Ideas:

A simple simulation of the stack into the stack operation, the elements sequentially into the stack. The stack is then sequentially based on the order entered. For example, the given stack order is 1 4 3 7 6 2 5, first put 1 into the stack, found that the first in the input data is exactly 1. will be 1 out of the stack, the following is 4, because the stack is empty, and just put 1 into the stack, and then 2 3 4 sequentially into the stack, when the stack top element is 4, and the second element of the input data equal, 4 out of the stack. Then 3 out of the stack, the next processing 7, because the top element of the stack is 2,7 greater than 2, so the next 5 6 7 into the stack (the element in the stack is 7 6 5 2). Then the top element of the stack is equal to 7 of the input data, 7 out of the stack, and then 6 is equal, 6 out of the stack. At this point the stack element (5 2), the input data to be judged is 2 5. 2 compared to the top of the stack 2<5, so this sequence is not possible out of the stack order.

Write an algorithm to determine if the stack order is correct

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.