You are viewing a single comment's thread. Return to all comments →
JAVA public class Solution {
private static class MyStack{ private Stack<String> stack1; private Stack<String> stack2; public MyStack(){ stack1 = new Stack<>(); stack2 = new Stack<>(); } public void push(String val){ stack1.push(val); } private void exchange(){ while(!stack1.isEmpty()){ stack2.push(stack1.pop()); } } public String pop(){ if(stack2.isEmpty()){ exchange(); } return stack2.pop(); } public String peek(){ if(stack2.isEmpty()){ exchange(); } return stack2.peek(); } } public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner input = new Scanner(System.in); MyStack ans = new MyStack(); while(input.hasNext()){ String in = input.nextLine(); if(in.split(" ").length>1){ ans.push(in.split(" ")[1]); }else{ if(in.equals("2")){ ans.pop(); }else if(in.equals("3")){ System.out.println(ans.peek()); } } } input.close(); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
JAVA public class Solution {
}