day10|232,

232.力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

之前学java的时候没有学过java的栈有哪些指令,现学习一下:

这道题要两个栈实现队列,那么就是一个栈负责进,一个栈负责出,出的那个里没东西了,进的那个就pop().出的那个再push(),想通就比较简单了。

class MyQueue {
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;

    public MyQueue() {
        stackIn = new Stack();
        stackOut = new Stack();
    }
    
    public void push(int x) {
       stackIn.push(x);
    }
    
    public int pop() {
       if(stackOut.isEmpty()){
           while(!stackIn.isEmpty()){
               stackOut.push(stackIn.pop());
           }
       }
       return stackOut.pop();
    }
    
    public int peek() {
         if(stackOut.isEmpty()){
           while(!stackIn.isEmpty()){
               stackOut.push(stackIn.pop());
           }
       }
       return stackOut.peek();
    }
    
    public boolean empty() {
      if(stackIn.isEmpty() && stackOut.isEmpty()){
          return true;
      }
      return false;
      
    }


}


/**
 * 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.力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

我自己的方法:
queue1作为主队,queue2作为备用。

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() {
        while(queue1.size()!= 1){
            queue2.add(queue1.poll());
        }
        int result = queue1.poll();
        while(!queue2.isEmpty()){
            queue1.add(queue2.poll());
        }
        return result;
    }
    
    public int top() {
        while(queue1.size()!= 1){
            queue2.add(queue1.poll());
        }
        int result = queue1.peek();
        queue2.add(queue1.poll());
        while(!queue2.isEmpty()){
            queue1.add(queue2.poll());
        }
        return result;
    }
    
    public boolean empty() {
        if(queue1.isEmpty() && queue2.isEmpty()){
            return true;
        }
        return false;
    }
}

/**
 * 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();
 */

用随想录的方法更容易理解,

class MyStack {
    //q1作为主要的队列,其元素排列顺序和出栈顺序相同
    Queue<Integer> q1 = new ArrayDeque<>();
    //q2仅作为临时放置
    Queue<Integer> q2 = new ArrayDeque<>();

    public MyStack() {

    }
    //在加入元素时先将q1中的元素依次出栈压入q2,然后将新加入的元素压入q1,再将q2中的元素依次出栈压入q1
    public void push(int x) {
        while (q1.size() > 0) {
            q2.add(q1.poll());
        }
        q1.add(x);
        while (q2.size() > 0) {
            q1.add(q2.poll());
        }
    }

    public int pop() {
        return q1.poll();
    }

    public int top() {
        return q1.peek();
    }

    public boolean empty() {
        return q1.isEmpty();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值