栈与队列 leetcode 232. 用栈实现队列 225. 用队列实现栈

题目内容

题目内容链接

java解答

  • 重要思想应该是应该用两个栈来实现队列
  • 更重要的是保证st1东西往st2放的时候一定要保证一个进栈一个出栈.[栈中不要出现同一个元素(数值相等的两个元素不是同一个元素)在两个栈中都有]
class MyQueue {
    private Stack<Integer> st1;// st1正常栈
    private Stack<Integer> st2;// 把st1栈全一个一个出给栈st2  那么他其实就是我们眼中的栈(用栈实现的)
    /** Initialize your data structure here. */
    public MyQueue() {
        st1=new Stack<Integer>(); //开辟空间
        st2=new Stack<Integer>(); 开辟空间
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        st1.push(x);  //直接st1正常接收进栈

    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(!st2.isEmpty()){  //名义上队列不空,直接弹出
            return st2.pop();
        }else{
            //把st1的元素全部出队放进st1
            while(!st1.isEmpty()){
                st2.push(st1.pop()); 
            }
            
        }
        return st2.pop();

    }
    
    /** Get the front element. */
    public int peek() {
        //返回st2的栈顶元素就行,如果st2不空,,空了再把st1的元素全部出队放进st1
          if(!st2.isEmpty()){
            return st2.peek();
        }else{
             //把st1的元素全部出队放进st1
            while(!st1.isEmpty()){
                st2.push(st1.pop());
            }
        }
        return st2.peek();
        
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        if(st2.isEmpty()&&st1.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();
 */

题目内容

题目链接

java解答(参考第一个大佬把自己复杂的if else改进了)

  • 重点:其实这个不能和栈一样直接变成队列,要求只要每次q1进个元素,就让q2所有的元素全部出队,出队的元素全部入队q1,然后q1 q2交换,这样去实现栈 . q1在没有元素进队时候始终为空
class MyStack {
    private Queue<Integer> q1;
    private Queue<Integer> q2;//实际就是我么我们眼中的栈 但并不是简单的把q1直接放进去

    /** Initialize your data structure here. */
    public MyStack() {
        q1=new LinkedList<Integer>(); //开辟空间
        q2=new LinkedList<Integer>(); //开辟空间
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        q1.offer(x);
        while(!q2.isEmpty()){
            q1.offer(q2.poll());
        }
        //q1  q2互换
        Queue temp=q2;
        q2=q1;
        q1=temp;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return q2.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return q2.peek();

    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return q2.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();
 */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值