-
Notifications
You must be signed in to change notification settings - Fork 2
/
0155-min-stack.rs
52 lines (46 loc) · 1.05 KB
/
0155-min-stack.rs
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
struct MinStack {
min: i32,
stack: Vec<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {
/** initialize your data structure here. */
fn new() -> Self {
MinStack {
min: std::i32::MAX,
stack: Vec::new(),
}
}
fn push(&mut self, x: i32) {
if x <= self.min {
self.stack.push(self.min);
self.min = x;
}
self.stack.push(x);
}
fn pop(&mut self) {
let popped = self.stack.pop().unwrap();
if popped == self.min {
self.min = self.stack.pop().unwrap();
}
}
fn top(&self) -> i32 {
*self.stack.last().unwrap()
}
fn get_min(&self) -> i32 {
self.min
}
}
fn main() {
let mut stack = MinStack::new();
stack.push(-2);
stack.push(0);
stack.push(-3);
assert_eq!(-3, stack.get_min());
stack.pop();
assert_eq!(0, stack.top());
assert_eq!(-2, stack.get_min());
}