反转栈或队列中的元素

(一)Implement Stack using Queues

题目:用队列的方式实现栈的操作;

解答:用两个队列的方式实现;

优化:使用一个队列,但是在push操作时,注意将元素顺序反转:

             public void push(int x) {
                   queue1.offer(x);

                   for (int i = 1; i <queue.size(); i++) {

                           queue.add(queue.poll());

                   }
             }           

代码:

class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    /** Initialize your data structure here. */
    public MyStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue1.offer(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        int size = queue1.size();
        for (int i = 0; i < size - 1; i++) {
            int node = queue1.poll();
            queue2.offer(node);
        }
        int res = queue1.poll();
         while (!queue2.isEmpty()) {
            int node = queue2.poll();
            queue1.offer(node);
        }
        return res;
    }
    
    /** Get the top element. */
    public int top() {
        int size = queue1.size();
        for (int i = 0; i < size - 1; i++) {
            int node = queue1.poll();
            queue2.offer(node);
        }
        int res = queue1.poll();
        queue2.offer(res);
        while (!queue2.isEmpty()) {
            int node = queue2.poll();
            queue1.offer(node);
        }
        return res;
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue1.isEmpty();
    }
}


(二)Implement Queue using Stacks

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

题目:使用栈来实现队列的操作;

解答:使用两个栈来实现栈内元素的逆序;

代码:

class MyQueue {
    Stack <Integer> stack1;
    Stack <Integer> stack2; //store the elements as the queue's order;
    /** Initialize your data structure here. */
    public MyQueue() {
        stack1 = new Stack<Integer>();
        stack2 = new Stack<Integer>();
    }   
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        while (!stack2.isEmpty()) {
            stack1.push(stack2.pop());
        }
        stack1.push(x);
        
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
        
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return stack2.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        return stack2.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack2.isEmpty();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值