• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

刷题day10栈和队列 | 理论基础 、 232.用栈实现队列 、 225. 用队列实现栈

武飞扬头像
Shan_Shi
帮助1

理论基础

文章讲解

232.用栈实现队列

题目链接/文章讲解/视频讲解

思路: 注意两个栈实现,出栈或取栈顶元素时从outStack(inStack反压栈)中取值,保证队列的先入先出特性。
在push数据的时候,只要数据放进输入栈就好,但在pop的时候,输出栈如果为空,就把进栈数据全部导入进来(注意是全部导入),再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了。

class MyQueue {
	private Deque<Integer> inStack;
	private Deque<Integer> outStack;
	
    public MyQueue() {
    	inStack = new LinkedList<>();
    	outStack = new LinkedList<>();
    }
    public void push(int x) {
    	inStack.push(x);
    }
    public int pop() {
    	if(outStack.isEmpty()) {
    		while(!inStack.isEmpty()) {
    			outStack.push(inStack.pop());
    		}
    	}
    	return outStack.pop();
    }  
    public int peek() {
    // 可以直接复用上面实现的pop()
        // int res = this.pop();
    	// outStack.push(res);
    	// return res;
    	if(outStack.isEmpty()) {
    		while(!inStack.isEmpty()) {
    			outStack.push(inStack.pop());
    		}
    	}
    	return outStack.peek();
    }
    public boolean empty() {
    	return inStack.isEmpty() && outStack.isEmpty();
    }
}
学新通

225. 用队列实现栈

题目链接/文章讲解/视频讲解

思路:

  1. 使用两个队列,queue2 当作备份,辅助 queue1 弹出队尾元素,保证后入先出特性
    学新通
//方法1. 两个队列
class MyStack {
    // Deque 接口继承了 Queue 接口
    // 所以 Queue 中的 add、poll、peek等效于 Deque 中的 addLast、pollFirst、peekFirst
    Deque<Integer> que1; // 和栈中保持一样元素的队列
    Deque<Integer> que2; // 辅助队列
    
    public MyStack() {
        que1 = new ArrayDeque<>();
        que2 = new ArrayDeque<>();
    }
    
    public void push(int x) {
        que1.addLast(x);
    }
    
    public int pop() {
        int size = que1.size();
        size--;
        // 将 que1 导入 que2 ,但留下最后一个值
        while (size-- > 0) {
            que2.addLast(que1.pollFirst());
        }
        int res = que1.pollFirst();
        // 将 que2 对象的引用赋给了 que1 ,此时 que1,que2 指向同一个队列
        que1 = que2;
        // 如果直接操作 que2,que1 也会受到影响,所以为 que2 分配一个新的空间
        que2 = new ArrayDeque<>();
        return res;
    }
    
    public int top() {
        return que1.peekLast();
    }
    
    public boolean empty() {
        return que1.isEmpty();
    }
}

方法2:一个队列实现
class MyStack {

    Queue<Integer> queue;

    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        int n = queue.size();
        queue.offer(x);
        for (int i = 0; i < n; i  ) {
            queue.offer(queue.poll());
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgghkhb
系列文章
更多 icon
同类精品
更多 icon
继续加载