my
class MinStack {
private LinkedList link;
/** initialize your data structure here. */
/*
MinStack()
{
link = new Deque();
}
*/
public MinStack() {
link = new LinkedList();
}
public void push(int val) {
link.addFirst(val);
}
public void pop() {
link.pollFirst();
}
public int top() {
return ((Integer)link.peekFirst());
}
public int getMin() {
int min = Integer.MAX_VALUE;
Iterator it = link.iterator();
while(it.hasNext())
{
Integer obj = (Integer)it.next();
if(obj < min)
{
min = obj;
}
}
return min;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
2
class MinStack {
LinkedList<Integer> link;
/** initialize your data structure here. */
/*
MinStack()
{
link = new Deque();
}
*/
public MinStack() {
link = new LinkedList<Integer>();
}
public void push(int val) {
link.addFirst(val);
}
public void pop() {
link.pollFirst();
}
public int top() {
return (link.peekFirst());
}
public int getMin() {
int min = Integer.MAX_VALUE;
Iterator<Integer> it = link.iterator();
while(it.hasNext())
{
Integer obj = it.next();
if(obj < min)
{
min = obj;
}
}
return min;
}
}
官方
class MinStack {
Deque<Integer> xStack;
Deque<Integer> minStack;
/** initialize your data structure here. */
/*
MinStack()
{
link = new Deque();
}
*/
public MinStack() {
xStack = new LinkedList<Integer>();
minStack = new LinkedList<Integer>();
minStack.addFirst(Integer.MAX_VALUE);
}
public void push(int val) {
xStack.addFirst(val);
minStack.addFirst(Math.min(val,minStack.peekFirst()));
}
public void pop() {
xStack.pollFirst();
minStack.pollFirst();
}
public int top() {
return (xStack.peekFirst());
}
public int getMin() {
return minStack.peekFirst();
}
}