-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path155.min-stack.0.rs
55 lines (47 loc) · 1.07 KB
/
155.min-stack.0.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
/*
* @lc app=leetcode id=155 lang=rust
*
* [155] Min Stack
*/
struct MinStack {
vec: Vec<i32>,
min_vec: 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{
vec: Vec::new(),
min_vec: Vec::new(),
}
}
fn push(&mut self, x: i32) {
self.vec.push(x);
if self.min_vec.len() == 0 || self.get_min() >= x {
self.min_vec.push(x);
}
}
fn pop(&mut self) {
if self.vec.pop().unwrap() == self.get_min() {
self.min_vec.pop();
}
}
fn top(&self) -> i32 {
*self.vec.last().unwrap()
}
fn get_min(&self) -> i32 {
*self.min_vec.last().unwrap()
}
}
/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(x);
* obj.pop();
* let ret_3: i32 = obj.top();
* let ret_4: i32 = obj.get_min();
*/