Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Push (x)-push element x onto stack.
Pop ()--Removes the element on top of the stack.
Top ()--Get the top element.
Getmin ()--Retrieve The minimum element in the stack.
Problem Solving Ideas:
The topic is the Java language Programming-Basic article of the original problem, modified can be, Java implementation is as follows:
public class Minstack {private int[] elements;private int size;public minstack () {elements = new int[16];} public void push (int x) {if (size >= elements.length) {int[] temp = new int[elements.length * 2]; System.arraycopy (elements, 0, temp, 0, elements.length); elements = temp;} elements[size++] = x;} public void Pop () {size--;} public int Top () {return elements[size-1];} public int getmin () {int min=integer.max_value;for (int i=0;i<size;i++) if (Min>elements[i]) min=elements[i]; return min;}}
Java for Leetcode 155 Min Stack