Print the order of push and POP based on the stack's inbound sequence and outbound sequence-Amazon pen questions

Source: Internet
Author: User

This is the question of Amazon's online technical test:

Go directly to the code, which is described in the Code. You only need to implement the calculateoperationsequence () method.

Package amazon2;/* enter your code here. read input from stdin. print Output to stdout */import Java. util. *; import Java. util. stack; import Java. util. stringtokenizer;/*** print the push and pop Sequences Based on the stack's outbound and inbound stack sequences. The position of push and pop must follow the position of the element in originalarray. For example, * originalarray: 1 2 3 4 * resultarray: 4 3 2 1 * putput: push1 | push2 | push3 | push4 | pop4 | POP3 | pop2 | pop1 ** algorithm idea: * stack // value of the elements stored in the stack; stackpos: Index of the elements stored in the stack in originalarray * While (true) {* If (the stack is empty) {* If (originalarray contains elements) {* an element in originalarray is added to the stack; * This element in originalarray is included in the stack stackpos; *} else {* break; // description at this time *} else {// the stack is not empty * If (the top element of the stack is equal to the current element in resultarray) {* Pop (); *} else {* If (in originalarray Element) {* an element in originalarray is added to the stack; * This element in originalarray is included in the stack stackpos; *} else {* return "NONE "; *} ** @ author songjie **/public class solution2 {private string calculateoperationsequence (INT [] originalarray, int [] resultarray) {// your code is hereif (originalarray = NULL | resultarray = NULL | originalarray. length = 0 | resultarray. length = 0 | originalarray. length! = Resultarray. length) {Throw new illegalargumentexception ("invalid arguments");} stringbuffer sb = new stringbuffer (""); stack <integer> stack = new stack <integer> (); stack <integer> stackpos = new stack <integer> (); int originalpos = 0; int resultpos = 0; while (true) {If (stack. isempty () {// stack is emptyif (originalpos> = originalarray. length) break; stack. push (originalarray [originalpos]); stackpos. push (originalpos ++); sb. append ("push" + originalpos + "|");} else {// stack is not emptyint peek = stack. peek (); If (resultarray [resultpos] = PEEK) {stack. pop (); sb. append ("pop" + (stackpos. pop () + 1) + "|"); resultpos ++;} else if (originalpos <originalarray. length) {stack. push (originalarray [originalpos]); stackpos. push (originalpos ++); sb. append ("push" + originalpos + "|");} else {return "NONE" ;}} return sb. tostring (). substring (0, sb. length ()-1);} public static void main (string [] ARGs) {solution2 solution = new solution2 (); specified duration = new partition (system. in); While (partial. hasnextline () {string strline1 = bytes. nextline (); stringtokenizer stringtokenizer1 = new stringtokenizer (strline1); // initialize the original arrayint arraylength = trim (); int [] originalarray = new int [arraylength]; for (INT I = 0; I <arraylength; I ++) {originalarray [I] = integer. parseint (stringtokenizer1.nexttoken ();} // initialize the result arraystring strline2 = response. nextline (); stringtokenizer stringtokenizer2 = new stringtokenizer (strline2); arraylength = trim (); int [] resultarray = new int [arraylength]; for (Int J = 0; j <arraylength; j ++) {resultarray [J] = integer. parseint (stringtokenizer2.nexttoken ();} string operationsequence = solution. calculateoperationsequence (originalarray, resultarray); system. out. println (operationsequence );}}}

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.