LeetCode | 232. Implement Queue using Stacks, 225. Implement Stack using Queues

232. Implement Queue using Stacks

Link: https://leetcode.com/problems/implement-queue-using-stacks/

Description

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

Implement the MyQueue class:

  • void push(int x) Pushes element x to the back of the queue.
  • int pop() Removes the element from the front of the queue and returns
    it.
  • int peek() Returns the element at the front of the queue.
  • boolean empty() Returns true if the queue is empty, false otherwise.

Notes:

  • You must use only standard operations of a stack, which means only
    push to top, peek/pop from top, size, and is empty operations are
    valid.
  • Depending on your language, the stack may not be supported natively.
    You may simulate a stack using a list or deque (double-ended queue)
    as long as you use only a stack’s standard operations.

Approach

push(int x)

  • Push the element to stack1.

pop()

  • If stack2 is empty, move all the elements of stack1 to stack2. After that, the first element in stack1 is the last emelent stack2. Pop the last element.
  • If stack2 is not empty, pop the last element.

Solution

class MyQueue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;
    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty())
                stack2.push(stack1.pop());
        }
        return stack2.pop();
    }
    
    public int peek() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty())
                stack2.push(stack1.pop());
        }
        return stack2.peek();
    }
    
    public boolean empty() {
        return stack1.isEmpty() && stack2.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();
 */

225. Implement Stack using Queues

Link: https://leetcode.com/problems/implement-stack-using-queues/

Description

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

  • void push(int x) Pushes element x to the top of the stack.
  • int pop() Removes the element on the top of the stack and returns it.
  • int top() Returns the element on the top of the stack.
  • boolean empty() Returns true if the stack is empty, false otherwise.

Notes:

  • You must use only standard operations of a queue, which means that
    only push to back, peek/pop from front, size and is empty operations
    are valid.
  • Depending on your language, the queue may not be supported natively.
    You may simulate a queue using a list or deque (double-ended queue)
    as long as you use only a queue’s standard operations.

Approach

Use 2 queues

push()
  • Push the element in stack1.
pop()
  • Move all the elments in stack1 to stack2, except the last element.
  • pop the last item, and let result equals to the last item.
  • Let stack1 equals to stack2, and reset stack2 as an empty stack.

Use 1 queue

pop()
  • compute the size of the queue.
  • traverse the queue, move the element on the top of the queue to the end of the queue except the last element of the original queue.
  • Pop the last element.

Solution

//Use 1 queue
class MyStack {
    Queue<Integer> queue1;
    public MyStack() {
        queue1 = new ArrayDeque<>();
    }
    
    public void push(int x) {
        queue1.add(x);
    }
    
    public int pop() {
        int size = queue1.size();
        while (size > 1) {
            int temp = queue1.remove();
            queue1.add(temp);
            size--;
        }
        return queue1.remove();
    }
    
    public int top() {
        int size = queue1.size();
        int result = -1;
        while (size > 0) {
            int temp = queue1.remove();
            queue1.add(temp);
            size--;
            if (size == 0)
                result = temp;
        }
        
        return result;
    }
    
    public boolean empty() {
        return queue1.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();
 */
//Use 2 queues
class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        queue1 = new ArrayDeque<>();
        queue2 = new ArrayDeque<>();
    }
    
    public void push(int x) {
        queue1.add(x);
    }
    
    public int pop() {
        int temp = -1;
        while(!queue1.isEmpty()) {
            temp = queue1.remove();
            if (!queue1.isEmpty())
                queue2.add(temp);
        }
        Queue<Integer> tempQ = queue1;
        queue1 = queue2;
        queue2 = tempQ;
        return temp;
    }
    
    public int top() {
        int temp = -1;
        while(!queue1.isEmpty()) {
            temp = queue1.remove();
            queue2.add(temp);
        }
        Queue<Integer> tempQ = queue1;
        queue1 = queue2;
        queue2 = tempQ;
        return temp;
    }
    
    public boolean empty() {
        return queue1.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();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值