forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
56 lines (47 loc) · 1.12 KB
/
Solution.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
53
54
55
56
struct MinStack {
items: Vec<i32>,
min: 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 {
items: Vec::new(),
min: Vec::new(),
}
}
fn push(&mut self, x: i32) {
self.items.push(x);
match self.min.last() {
Some(min) => {
if min >= &x {
self.min.push(x);
}
},
None => self.min.push(x),
}
}
fn pop(&mut self) {
if &self.items.pop().unwrap() == self.min.last().unwrap() {
self.min.pop();
}
}
fn top(&self) -> i32 {
*self.items.last().unwrap()
}
fn get_min(&self) -> i32 {
*self.min.last().unwrap()
}
}
/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(val);
* obj.pop();
* let ret_3: i32 = obj.top();
* let ret_4: i32 = obj.get_min();
*/