美文网首页
算法记录 | Day09(栈、队列 01)

算法记录 | Day09(栈、队列 01)

作者: perry_Fan | 来源:发表于2022-11-03 17:58 被阅读0次

    【232. 用栈实现队列】

    请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

    实现 MyQueue 类:

    void push(int x) 将元素 x 推到队列的末尾
    int pop() 从队列的开头移除并返回元素
    int peek() 返回队列开头的元素
    boolean empty() 如果队列为空,返回 true ;否则,返回 false
    说明:

    你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
    你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

    class MyQueue {
    
        Stack<Integer> stackIn;
        Stack<Integer> stackOut;
    
        /** Initialize your data structure here. */
        public MyQueue() {
            stackIn = new Stack<>(); // 负责进栈
            stackOut = new Stack<>(); // 负责出栈
        }
        
        /** Push element x to the back of queue. */
        public void push(int x) {
            stackIn.push(x);
        }
        
        /** Removes the element from in front of queue and returns that element. */
        public int pop() {    
            dumpstackIn();
            return stackOut.pop();
        }
        
        /** Get the front element. */
        public int peek() {
            dumpstackIn();
            return stackOut.peek();
        }
        
        /** Returns whether the queue is empty. */
        public boolean empty() {
            return stackIn.isEmpty() && stackOut.isEmpty();
        }
    
        // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
        private void dumpstackIn(){
            if (!stackOut.isEmpty()) return; 
            while (!stackIn.isEmpty()){
                    stackOut.push(stackIn.pop());
            }
        }
    }
    

    【225. 用队列实现栈】

    使用队列实现栈的下列操作:

    push(x) -- 元素 x 入栈
    pop() -- 移除栈顶元素
    top() -- 获取栈顶元素
    empty() -- 返回栈是否为空
    注意:

    你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
    你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
    你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

    class MyStack{
        Queue<Integer> queue1; // 和栈中保持一样元素的队列
        Queue<Integer> queue2; // 辅助队列
    
        public MyStack(){
            queue1 = new LinkedList<>();
            queue2 = new LinkedList<>();
        }
        public void push(int x){
            queue2.offer(x); // 先放在辅助队列中
            while(!queue1.isEmpty()){
                queue2.offer(queue1.poll());
            }
            Queue<Integer> queueTemp;
            queueTemp = queue1;
            queue1 = queue1;
            queue2 = queueTemp; // 最后交换queue1 和queue2,将元素都放到 queue1 中。
        }
    
        public int pop(){
            return queue1.poll();  // 因为 queue1 中的元素和栈中的保持一致,所以只看 queue1
        }
    
        public int top(){
            return queue1.peek();
        }
    
        public boolean empty(){
            return queue1.isEmpty();
        }
        
    }
    

    相关文章

      网友评论

          本文标题:算法记录 | Day09(栈、队列 01)

          本文链接:https://www.haomeiwen.com/subject/eilutdtx.html