forked from haobinaa/DataStructure-DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.java
More file actions
52 lines (44 loc) · 1.19 KB
/
MinStack.java
File metadata and controls
52 lines (44 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.haobin.leetcode.stack;
import java.util.Stack;
/**
* @Author HaoBin
* @Create 2020/1/15 20:16
* @Description: 最小栈
*
* 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
*
* push(x) -- 将元素 x 推入栈中。
* pop() -- 删除栈顶的元素。
* top() -- 获取栈顶元素。
* getMin() -- 检索栈中的最小元素。
*
*
* 思路: 空间换时间, 用另一个辅助栈的栈顶始终是当前栈的最小元素(两个栈入栈出栈元素保持一致.)
**/
public class MinStack {
Stack<Integer> dataStack;
Stack<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
dataStack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
if (minStack.empty() || x < minStack.peek() ) {
minStack.push(x);
} else {
minStack.push(minStack.peek());
}
dataStack.push(x);
}
public void pop() {
minStack.pop();
dataStack.pop();
}
public int top() {
return dataStack.peek();
}
public int getMin() {
return minStack.peek();
}
}