The min stack

Problem description

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Examples

1
2
3
4
5
6
7
8
9
Example 1:
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.

Solution

  逻辑没问题,但就是怎么就AC不了。还是那个老问题。以前是list里添加list要new。这道题是同样问题。List添加对象时,也要每次new,每次靠 = 修改对象再添加。这样list内的对象会都一样。

Code

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
 class MinStack {

Stack<Integer> mStack;
Stack<Integer> mMinStack;

public MinStack() {
mStack = new Stack<>();
mMinStack = new Stack<>();
}

public void push(int x) {
mStack.push(x);
if (mMinStack.size() == 0 || x < mMinStack.peek()) {
mMinStack.push(x);
} else {
mMinStack.push(mMinStack.peek());
}
}

public void pop() {
if (!mStack.isEmpty()) {
mStack.pop();
mMinStack.pop();
}
}

public int top() {
return mStack.peek();
}

public int getMin() {
return mMinStack.peek();
}
}
文章目录
  1. 1. Problem description
    1. 1.1. Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
  2. 2. Examples
  3. 3. Solution
  4. 4. Code
|