1. 程式人生 > >演算法題014 -- [Min Stack] by java

演算法題014 -- [Min Stack] by java

題目

設計一個支援push、pop、top和能返回最小值stack中最小值的stack

舉例

MinStack minStack = new MinStack(); 
minStack.push(-2); 
minStack.push(0); 
minStack.push(-3); 
minStack.getMin(); –> Returns -3. 
minStack.pop(); 
minStack.top(); –> Returns 0. 
minStack.getMin(); –> Returns -2.

分析

考察的完全是程式碼功底…
這裡我分別是用陣列和類連結串列的資料結構來實現。

程式碼

package algorithm014;

import java.util.Arrays;

public class Algorithm014 {
	
	public static void main(String[] args) {
//		testCode(); //測試程式碼在最下面
	}
}

interface IMinStack<E>{
	E push(E item);
	E pop();
	E top();
	E getMin();
}

abstract class AbsMinStack<E> implements IMinStack<
E>
{ int realElementCout; E top; E min; @Override public E top() { return top; } @Override public E getMin() { return min; } public int size() { return realElementCout; } } /** * 陣列實現,主要在於 arr 的擴容 * @author looper * */ class ArrayMinStack extends AbsMinStack<Integer>
{ private static final int DEFAULT_SIZE = 8; private Integer[] arr = new Integer[DEFAULT_SIZE]; @Override public Integer push(Integer item) { if(item != null) { realElementCout++; if(realElementCout > arr.length) arr = Arrays.copyOf(arr, arr.length + DEFAULT_SIZE); arr[realElementCout-1] = item; top = item; if(min == null) min = item; else if(min > item) { min = item; } } return item; } @Override public Integer pop() { if(realElementCout >=1) { realElementCout--; if(realElementCout <= 0) top = null; else top = arr[realElementCout-1]; return arr[realElementCout]; }else { top = null; return null; } } } /** * 連結串列實現,主要在於 變數的切換賦值 * @author looper * */ class LinkedMinStack extends AbsMinStack<Integer>{ ListNode preNode = null; ListNode tailNode = null; @Override public Integer push(Integer item) { if(item != null) { realElementCout++; top = item; if(min == null) { min = item; }else if(min > item) { min = item; } preNode = tailNode; tailNode = new ListNode(item); tailNode.pre = preNode; } return item; } @Override public Integer pop() { if(tailNode == null) { return null; } int pop = tailNode.content; tailNode = preNode; if(tailNode != null) { top = tailNode.content; }else top = null; if(preNode != null) preNode = preNode.pre; return pop; } class ListNode{ Integer content; ListNode pre; public ListNode() {} public ListNode(Integer content) { this.content = content; } } } //private static void testCode() { // Integer top; // Integer min; // AbsMinStack<Integer> arrayMinStack = new ArrayMinStack(); // min = arrayMinStack.getMin(); // top = arrayMinStack.top(); // System.out.println(top +""); // System.out.println(min+""); // arrayMinStack.push(0); // arrayMinStack.push(-2); // arrayMinStack.push(3); // arrayMinStack.push(1); // min = arrayMinStack.getMin(); // top = arrayMinStack.top(); // System.out.println(top +""); // System.out.println(min+""); // System.out.println(arrayMinStack.pop() +""); // System.out.println(arrayMinStack.pop() +""); // System.out.println(arrayMinStack.pop() +""); // System.out.println(arrayMinStack.pop() +""); // System.out.println(arrayMinStack.pop() +""); // arrayMinStack.push(5); // System.out.println(arrayMinStack.pop() +""); // System.out.println("==="); // AbsMinStack<Integer> linkedMinStack = new LinkedMinStack(); // min = linkedMinStack.getMin(); // top = linkedMinStack.top(); // System.out.println(top +""); // System.out.println(min+""); // linkedMinStack.push(0); // linkedMinStack.push(-1); // linkedMinStack.push(3); // linkedMinStack.push(-12); // linkedMinStack.push(30); // linkedMinStack.push(5); // min = linkedMinStack.getMin(); // top = linkedMinStack.top(); // System.out.println(top +""); // System.out.println(min+""); // System.out.println(linkedMinStack.pop() +""); // System.out.println(linkedMinStack.pop() +""); // System.out.println(linkedMinStack.pop() +""); // System.out.println(linkedMinStack.pop() +""); // System.out.println(linkedMinStack.pop() +""); // System.out.println(linkedMinStack.pop() +""); // System.out.println(linkedMinStack.pop() +""); // linkedMinStack.push(5); // System.out.println(linkedMinStack.pop() +""); //}