LeetCode刷題EASY篇實現mini stack
阿新 • • 發佈:2018-12-13
題目
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.
Example:
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.
我的思路
利用基本型別陣列or連結串列實現。但是發現提示裡面利用已有的stack實現。好,這樣更方便
先複習一些jdk的stack是怎麼實現的?是用陣列實現的,有個top指標
/** * 使用陣列實現堆疊,包括入棧、出棧、獲取堆疊長度、 * @author Adair */ publicclass Stack { Object[] data; int maxSize; //棧頂位置 int top; public Stack(int maxSize) { this.maxSize = maxSize; data = new Object[maxSize]; top = -1; } /** * 獲取堆疊長度 * @return 堆疊長度 */ publicint getSize() { return maxSize; } /** * 返回棧中元素的個數 * @return 棧中元素的個數 */ publicint getElementCount() { return top; } /** * 判斷棧空 * @return 棧空 */ publicboolean isEmpty() { return top == -1; } /** * 判斷棧滿 * @return 棧滿 */ publicboolean isFull() { return top+1 == maxSize; } /** * 依次加入資料 * @param data 要加入的資料通訊 * @return 新增是否成功 */ publicboolean push(Object data) { if(isFull()) { System.out.println("棧已滿!"); returnfalse; } this.data[++top] = data; returntrue; } /** * 從棧中取出資料 * @return 取出的資料 */ public Object pop() throws Exception{ if(isEmpty()) { thrownew Exception("棧已空!"); } returnthis.data[top--]; } /** * 返回棧頂元素 * @return */ public Object peek() { returnthis.data[getElementCount()]; } publicstaticvoid main(String[] args) throws Exception { Stack stack=new Stack(1000); stack.push(new String("1")); stack.push(new String("2")); stack.push(new String("3")); stack.push(new String("4")); stack.push(new String("5")); System.out.println(stack.peek()); while(stack.top>=0) { System.out.println(stack.pop()); } } }
繼續我們的題目,關鍵如何實現min功能。看似很簡單,其實不然,看我的程式碼,發現忽略了一個情況,新增元素的時候比較記錄最小值,沒有問題,但是pop的時候,stack中元素髮生變化了?如果彈出的是最小值,怎麼辦?感覺好像需要額外的空間記錄最小值,但是都記錄沒有意義,只是記錄最近一個最小值就可以。
class MinStack {
private Stack<Integer> stack;
private int min=Integer.MAX_VALUE;
/** initialize your data structure here. */
public MinStack() {
stack =new Stack<Integer>();
}
public void push(int x) {
if(x<min){
min=x;
}
stack.push(x);
}
public void pop() {
//pop只有元素髮生變化,重新計算min
int top=stack.pop();
if(top==min){
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
合理答案
在我的思路上,看了看別人的實現。利用stack記錄最近的一個最小值。出stack的時候, 如果出stack元素是當前最小值,那麼一起pop出第二個元素,且min等於此元素,因為這個元素就是當前的最小值了。
一個易錯點:push的時候,if條件是小於等於,不是小於
class MinStack {
private Stack<Integer> stack;
private int min=Integer.MAX_VALUE;
/** initialize your data structure here. */
public MinStack() {
stack =new Stack<Integer>();
}
public void push(int x) {
//注意等於情況,不可缺少
if(x<=min){
stack.push(min);
min=x;
}
stack.push(x);
}
public void pop() {
//pop只有元素髮生變化,重新計算min
int top=stack.pop();
if(top==min){
min=stack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/