問題描述:java
定義棧的數據結構,要求添加一個min 函數,可以獲得棧的最小元素。數據結構
要求函數min、push 以及pop 的時間複雜度都是O(1)。函數
雙倍空間實現:spa
保存2個棧,分別是元素和當前最小值。code
壓縮空間實現:element
?get
代碼實現:io
package oschina.IT100; /** * @project: oschina * @filename: IT2.java * @version: 0.10 * @author: JM Han * @date: 17:08 2015/10/20 * @comment: design a stack that has min function which could return the min element at anytime * @result: */ import java.util.Stack; class MinStack{ Stack<Integer> stacks, mins; MinStack(){ stacks = new Stack<Integer>(); mins = new Stack<Integer>(); } void push(Integer x){ stacks.push(x); //use <= instead of < for duplicate element if(mins.isEmpty() || x <= mins.peek()) mins.push(x); } void pop(){ Integer x = stacks.pop(); if(x == mins.peek()) mins.pop(); } Integer peek(){ return stacks.peek(); } Integer getMin(){ return mins.peek(); } } public class IT2 { public static void main(String[] args) { MinStack minStack = new MinStack(); minStack.push(6); minStack.push(2); minStack.push(3); minStack.push(1); minStack.push(1); minStack.push(5); minStack.push(8); System.out.println("Content of stack: \n" + minStack.stacks); System.out.println("min value of stack: \n" + minStack.getMin()); System.out.println("Content of mins: \n" + minStack.mins); minStack.pop(); minStack.pop(); minStack.pop(); System.out.println("Content of mins: \n" + minStack.mins); System.out.println("min value of stack: \n" + minStack.getMin()); } }
輸出:function
Content of stack: [6, 2, 3, 1, 1, 5, 8] min value of stack: 1 Content of mins: [6, 2, 1, 1] Content of mins: [6, 2, 1] min value of stack: 1