classMyQueue{ private Stack<Integer> iStack; private Stack<Integer> oStack; /** Initialize your data structure here. */ publicMyQueue(){ this.iStack = new Stack<>(); this.oStack = new Stack<>(); } /** Push element x to the back of queue. */ publicvoidpush(int x){ iStack.push(x); } /** Removes the element from in front of queue and returns that element. */ publicintpop(){ if (!oStack.empty()){ return oStack.pop(); }else { while (!iStack.empty()) { oStack.push(iStack.pop()); } return oStack.pop(); } } /** Get the front element. */ publicintpeek(){ if (!oStack.empty()){ return oStack.peek(); }else { while (!iStack.empty()) { oStack.push(iStack.pop()); } return oStack.peek(); } } /** Returns whether the queue is empty. */ publicbooleanempty(){ return iStack.empty()&& oStack.empty(); } }
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
Solution 2
思路二:一个栈作为中转站,一个作为最终数据存储区(类似于罗汉塔,小的环只能在大环上面👆)
faster than 100%;Memory Usage: 36.8 MB, less than 76.39% of Java online submissions for Implement Queue using Stacks.
Time Complexity: push O(n), pop/peek O(1), empty O(1)
/** * Initialize your data structure here. */ publicMyQueue(){ this.terminalStack = new Stack<>(); this.transferStack = new Stack<>(); }
/** * Push element x to the back of queue. */ publicvoidpush(int x){ // temporarily move terminalStack's element to transferStack if (terminalStack.empty()) { terminalStack.push(x); } else { while (!terminalStack.empty()) { transferStack.push(terminalStack.pop()); } terminalStack.push(x);
} // move back while (!transferStack.empty()) { terminalStack.push(transferStack.pop()); } }
/** * Removes the element from in front of queue and returns that element. */ publicintpop(){ return terminalStack.pop(); }
/** * Get the front element. */ publicintpeek(){ return terminalStack.peek(); }
/** * Returns whether the queue is empty. */ publicbooleanempty(){ return terminalStack.empty(); } }
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */
/** Initialize your data structure here. */ publicMyStack(){ q1 = new LinkedList<>(); q2 = new LinkedList<>(); }
/** Push element x onto stack. */ publicvoidpush(int x){ q1.add(x); top = x; }
/** Removes the element on top of the stack and returns that element. */ publicintpop(){ while(q1.size()>1){ top = q1.remove(); q2.add(top); } Queue<Integer> temp; temp = q1; q1 = q2; q2 = temp; return q2.remove(); }
/** Get the top element. */ publicinttop(){ return top; }
/** Returns whether the stack is empty. */ publicbooleanempty(){ return q1.isEmpty(); } }
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
/** * Removes the element on top of the stack and returns that element. */ publicintpop(){ return q1.remove(); }
/** * Get the top element. */ publicinttop(){ return q1.peek(); }
/** * Returns whether the stack is empty. */ publicbooleanempty(){ return q1.size() == 0; } }
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
Solution 3
思路:只用一个队列解决,每次新增新元素时将前面的元素pop出来再存入队列,即1<-2 to 2<-1, 2<-1<-3 to 1<-3<-2 to 3<-2<-1.
Runtime: 0 ms, faster than 100.00% of Java online submissions for Implement Stack using Queues.
Memory Usage: 36.7 MB, less than 69.68% of Java online submissions for Implement Stack using Queues.
Time complexity: push/empty/top O(1), peek/pop O(n)
/** * Initialize your data structure here. */ publicMyStack(){ this.queue = new LinkedList<>(); }
/** * Push element x onto stack. */ publicvoidpush(int x){ queue.add(x); int size = queue.size(); while (size > 1) { Integer first = queue.remove(); queue.add(first); size--; } }
/** * Removes the element on top of the stack and returns that element. */ publicintpop(){ return queue.remove(); }
/** * Get the top element. */ publicinttop(){ return queue.peek(); }
/** * Returns whether the stack is empty. */ publicbooleanempty(){ return queue.size() == 0; } }
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */