2
  • 堆栈和队列统称线性表

    • 简单的线性结构

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

  • 堆栈

    • 基本理解

    • 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();
            }
    
    }

菟潞寺沙弥
303 声望55 粉丝