-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMinStack.java
More file actions
72 lines (63 loc) · 1.68 KB
/
Copy pathMinStack.java
File metadata and controls
72 lines (63 loc) · 1.68 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.lintcode;
import java.util.Stack;
/**
*12. 带最小值操作的栈.
*
* 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。
你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。
*/
public class MinStack {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MinStack(){
// do intialization if necessary
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
/*
* @param number: An integer
* @return: nothing
*/
public void push(int number) {
// write your code here
if(stackMin.isEmpty()){
this.stackMin.push(number);
}else if(number <= this.min()){
this.stackMin.push(number);
}
this.stackData.push(number);
}
/*
* @return: An integer
*/
public int pop(){
// write your code here
if(this.stackData.isEmpty()){
throw new RuntimeException("Your stack is empty.");
}
int value = this.stackData.pop();
if(value == this.min()){
this.stackMin.pop();
}
return value;
}
/*
* @return: An integer
*/
public int min(){
// write your code here
if(this.stackMin.isEmpty()){
throw new RuntimeException("Your stack is empty.");
}
return this.stackMin.peek();
}
public static void main(String[] args) {
MinStack s = new MinStack();
s.push(1);
s.pop();
s.push(2);
s.push(3);
int i = s.min();
System.out.println(i);
}
}