双栈结构实现最小栈


title: 双栈结构实现最小栈
date: 2019-05-22 21:56:27
tags: [栈,algorithms]


说双栈之前先说一下我愚钝的做法,链表实现最小值存储,后面发现最小栈与我这个十分相似

实现思想

进栈时: 创建一个最小值链表,每次push元素的时候,和之前的最小值比较(当然,第一次push的就是最小的),与之前的最小值比较,小则头插最小值链表,大则执行下一步

退栈时: 每次pop时检测退栈的元素是否和链表头最小的元素相等,如果相等链表头元素也删除,这保证了链表头元素一定在栈实时的元素中是最小的

package stack;

import java.util.Arrays;

/**
 * @author wangchong
 * @date 2019/5/20 9:05
 * @email 876459397@qq.com
 * @优快云 https://blog.youkuaiyun.com/wfcn_zyq
 * @describe
 */
public class MinStack {
    int[] elementArray;
    int maxSize = 1;
    int currentSize;
    Node headNode;
    private class Node {
        int v;
        int index;
        Node nextNode;
    }
    /** initialize your data structure here. */
    public MinStack() {
        headNode = null;
        elementArray = new int[maxSize];
        currentSize = 0;
    }

    public void push(int x) {
        if (currentSize == maxSize) {
            int oldSize = maxSize;
            int newSize = oldSize << 1;
            maxSize = newSize;
            elementArray = Arrays.copyOf(elementArray,maxSize);
        }
        Node newNode = new Node();
        if (currentSize == 0) {
            newNode.v = x;
            newNode.index = currentSize;
            headNode = newNode;
            elementArray[currentSize++] = x;
        } else {
            if (x < headNode.v) {
                newNode.v = x;
                newNode.index = currentSize;
                newNode.nextNode = headNode;
                headNode = newNode;
            }
            elementArray[currentSize++] = x;
        }
    }

    public void pop() {
        if (currentSize == 0) {
            return;
        }
        if (top() == headNode.v && headNode.index == currentSize - 1) {
            headNode = headNode.nextNode;
        }
        currentSize--;
    }

    public int top() {
        if (currentSize == 0) {
            return 0;
        }
        return elementArray[currentSize - 1];
    }

    public int getMin() {
        return headNode.v;
    }
}
双栈结构

思想: 与链表的思想相同,只是结构不同

import java.util.Stack;

public class Code_02_GetMinStack {
   public static class MyStack1 {
      private Stack<Integer> stackData;
      private Stack<Integer> stackMin;

      public MyStack1() {       //Stack系统提供栈结构
         this.stackData = new Stack<Integer>();//数据栈
         this.stackMin = new Stack<Integer>();//栈顶为最小值
      }

      public void push(int newNum) {
         if (this.stackMin.isEmpty()) {
            this.stackMin.push(newNum);
         } else if (newNum <= this.getmin()) {
            this.stackMin.push(newNum);
         }
         this.stackData.push(newNum);
      }

      public int pop() {
         if (this.stackData.isEmpty()) {
            throw new RuntimeException("Your stack is empty.");
         }
         int value = this.stackData.pop();
         if (value == this.getmin()) {
            this.stackMin.pop();
         }
         return value;
      }

      public int getmin() {
         if (this.stackMin.isEmpty()) {
            throw new RuntimeException("Your stack is empty.");
         }
         return this.stackMin.peek();
      }
   }

   public static class MyStack2 {
      private Stack<Integer> stackData;
      private Stack<Integer> stackMin;

      public MyStack2() {
         this.stackData = new Stack<Integer>();
         this.stackMin = new Stack<Integer>();
      }

      public void push(int newNum) {
         if (this.stackMin.isEmpty()) {
            this.stackMin.push(newNum);
         } else if (newNum < this.getmin()) {
            this.stackMin.push(newNum);
         } else {
            int newMin = this.stackMin.peek();
            this.stackMin.push(newMin);//重复压入栈顶
         }
         this.stackData.push(newNum);
      }

      public int pop() {
         if (this.stackData.isEmpty()) {
            throw new RuntimeException("Your stack is empty.");
         }
         this.stackMin.pop();
         return this.stackData.pop();
      }

      public int getmin() {
         if (this.stackMin.isEmpty()) {
            throw new RuntimeException("Your stack is empty.");
         }
         return this.stackMin.peek();
      }
   }

   public static void main(String[] args) {
      MyStack1 stack1 = new MyStack1();
      stack1.push(3);
      System.out.println(stack1.getmin());
      stack1.push(4);
      System.out.println(stack1.getmin());
      stack1.push(1);
      System.out.println(stack1.getmin());
      System.out.println(stack1.pop());
      System.out.println(stack1.getmin());

      System.out.println("=============");

      MyStack1 stack2 = new MyStack1();
      stack2.push(3);
      System.out.println(stack2.getmin());
      stack2.push(4);
      System.out.println(stack2.getmin());
      stack2.push(1);
      System.out.println(stack2.getmin());
      System.out.println(stack2.pop());
      System.out.println(stack2.getmin());
   }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值