思路:
// @Title: 用栈实现队列 (Implement Queue using Stacks)
// @Author: qisiii
// @Date: 2024-01-02 22:07:30
// @Runtime: 0 ms
// @Memory: 40.3 MB
// @comment:
// @flag:
class MyQueue {
Stack<Integer> push=new Stack();
Stack<Integer> pop=new Stack();
public MyQueue() {
}
public void push(int x) {
while (!push.isEmpty()){
Integer obj = push.pop();
pop.push(obj);
}
push.push(x);
while (!pop.isEmpty()){
push.push(pop.pop());
}
}
public int pop() {
return push.pop();
}
public int peek() {
return push.peek();
}
public boolean empty() {
return push.isEmpty();
}
}
/**
* 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();
*/
+++ title = “用栈实现队列 (Implement Queue using Stacks)” draft = false +++
思路:
// @Title: 用栈实现队列 (Implement Queue using Stacks)
// @Author: qisiii
// @Date: 2024-09-14 11:33:18
// @Runtime: 0 ms
// @Memory: 40.3 MB
// @comment:
// @flag:
class MyQueue {
private Stack<Integer> popStack = new Stack();
private Stack<Integer> pushStack = new Stack();
public MyQueue() {
}
public void push(int x) {
pushStack.push(x);
}
public int pop() {
if (popStack.isEmpty()) {
while (!pushStack.isEmpty()) {
popStack.push(pushStack.pop());
}
}
return popStack.pop();
}
public int peek() {
if (popStack.isEmpty()) {
while (!pushStack.isEmpty()) {
popStack.push(pushStack.pop());
}
}
return popStack.peek();
}
public boolean empty() {
return pushStack.isEmpty() && popStack.isEmpty();
}
}
/**
* 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();
*/
思路:
// @Title: 用栈实现队列 (Implement Queue using Stacks)
// @Author: qisiii
// @Date: 2022-02-28 16:23:07
// @Runtime: 1 ms
// @Memory: 38.8 MB
// @comment:
// @flag:
class MyQueue {
private Stack<Integer> pushStack=new Stack();
private Stack<Integer> popStack=new Stack();
public MyQueue() {
}
public void push(int x) {
pushStack.push(Integer.valueOf(x));
}
public int pop() {
if(popStack.isEmpty()){
while(!pushStack.isEmpty()){
Integer v= pushStack.pop();
popStack.push(v);
}
}
if(popStack.isEmpty()){
return -1;
}
return popStack.pop();
}
public int peek() {
if(popStack.isEmpty()){
while(!pushStack.isEmpty()){
Integer v= pushStack.pop();
popStack.push(v);
}
}
return popStack.peek();
}
public boolean empty() {
return pushStack.isEmpty()&&popStack.isEmpty();
}
}
/**
* 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();
*/
思路:
// @Title: 用栈实现队列 (Implement Queue using Stacks)
// @Author: qisiii
// @Date: 2024-08-13 15:19:28
// @Runtime: 0 ms
// @Memory: 40.3 MB
// @comment:
// @flag:
class MyQueue {
private Stack<Integer> push=new Stack<>();
private Stack<Integer> pop=new Stack<>();
public MyQueue() {
}
public void push(int x) {
push.push(x);
}
public int pop() {
if(empty()){
return -1;
}
if(pop.isEmpty()){
while(!push.isEmpty()){
pop.push(push.pop());
}
}
return pop.pop();
}
public int peek() {
if(empty()){
return -1;
}
if(pop.isEmpty()){
while(!push.isEmpty()){
pop.push(push.pop());
}
}
return pop.peek();
}
public boolean empty() {
return push.isEmpty()&&pop.isEmpty();
}
}
/**
* 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();
*/