我的面试准备过程--队列与栈(更新中)

本文介绍如何使用栈和队列这两种基本数据结构,并探讨它们的实现方式,包括利用两个栈实现队列功能,以及利用两个队列实现栈功能。此外,还详细介绍了设计带有min方法的栈的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 堆栈和队列统称线性表

    • 简单的线性结构

      • 数组和链表可以实现这两种数据结构

  • 堆栈

    • 基本理解

    • DFS

      • 深度优先---按深度遍历

      • 递归转非递归

  • 队列

    • 基本理解

    • BFS

      • 广度优先---按层序遍历

  • 出入栈的合法性
    模拟出入栈的过程,不是入栈,就是出栈,不然就不合法

    public boolean isPossible(int[] in, int[] out){
        if(in.length != out.length){
            return false;
        }
        
        Stack<Integer> s = new Stack<>();
        for(int i = 0, j = 0;j < out.length; j++){
            //如果不是入栈的
            while(s.isEmpty() && s.peek() != out[j]){
                if(i >= in.length){
                    return false;
                }
                s.push(in[i++]);
            }
            //那就出栈
            s.pop();
        }
        
        return true;
    }
  • 两个栈实现队列

        public class MyQueue{
            Stack<Integer> s1 = new Stack<>();
            Stack<Integer> s2 = new Stack<>();
    
            public void push(int x){
                s1.push(x);
            }
    
            //负负得正
            public int pop(){
                if(s2.isEmpty()){
                    while(!s1.isEmpty()){
                        s2.push(s1.pop());
                    }
                }
                return s2.pop();
            }
    
            public int peek(){
                if(s2.isEmpty()){
                    while(!s1.isEmpty()){
                        s2.push(s1.pop());
                    }
                }
                return s2.peek();
            }
    
            public boolean empty(){
                return s1.isEmpty() && s2.isEmpty();
            }
    
        } 
  • 两个队列实现栈

    public class MyStack {
        Queue<Integer> queue1;
        Queue<Integer> queue2;
        /** Initialize your data structure here. */
        public MyStack() {
            queue1 = new LinkedList<>();
            queue2 = new LinkedList<>();
        }
        
        /** Push element x onto stack. */
        public void push(int x) {
            if(!queue1.isEmpty()){
                queue1.offer(x);
            }else{
                queue2.offer(x);
            }
        }
        
        /** Removes the element on top of the stack and returns that element. */
        public int pop() {
            if(queue1.size() != 0){
                while(queue1.size() > 1){
                    queue2.add(queue1.peek());
                    queue1.poll();
                }
                return queue1.remove();
            }else{
                while(queue2.size() > 1){
                    queue1.add(queue2.peek());
                    queue2.poll();
                }
                return queue2.remove();
            }
        }
        
        /** Get the top element. */
        public int top() {
            if(queue1.size() != 0){
                while(queue1.size() > 1){
                    queue2.add(queue1.poll());
                }
                int res = queue1.peek();
                queue2.add(queue1.poll());
                return res;
            }else{
                while(queue2.size() > 1){
                    queue1.add(queue2.poll());
                }
                int res = queue2.peek();
                queue1.add(queue2.poll());
                return res;
            }
        }
        
        /** Returns whether the stack is empty. */
        public boolean empty() {
            return queue1.isEmpty() && queue2.isEmpty();
        }
    }
  • 设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值。push、pop和min三个方法的时间复杂度必须为O(1)

    两种解法,解法一,将最小值存入自有的数据结构中,如下所示

    public class MyStack extends Stack<NodeWithMin>{
        
        public void push(int value){
            int newMin = Math.min(value, min());
            super.push(new NodeWithMin(value, newMin));
        }
        
        public int min(){
            if(this.isEmpty()){
                return Integer.MAX_VALUE;
            }else{
                return peek().min;
            }
        }
        
        public int pop(){
            super.pop().value;
        }
    }
    
    class NodeWithMin{
        int value;//原本的值
        int min;//最小值
        public NodeWithMin(int value, int min){
            this.value = value;
            this.min = min;
        }
    }

    解法二,用两个栈

    public class MyStack{
            Stack<Integer> s1 = new Stack<>();
            Stack<Integer> s2 = new Stack<>();
    
            public void push(int i){
                    s1.push(i);
                    if(s2.isEmpty() || min() >= i){
                            s2.push(i);
                    }
            }
    
            public int pop(){
                    if(s1.peek() == min()){
                            s2.pop();
                    }
    
                    return s1.pop();
            }
    
            public int min(){
                    return s2.peek();
            }
    
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值