mirror of https://github.com/doocs/leetcode.git
42 lines
974 B
Rust
42 lines
974 B
Rust
use std::collections::VecDeque;
|
|
struct MinStack {
|
|
stack: VecDeque<i32>,
|
|
min_stack: VecDeque<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 {
|
|
Self {
|
|
stack: VecDeque::new(),
|
|
min_stack: VecDeque::new(),
|
|
}
|
|
}
|
|
|
|
fn push(&mut self, x: i32) {
|
|
self.stack.push_back(x);
|
|
if self.min_stack.is_empty() || *self.min_stack.back().unwrap() >= x {
|
|
self.min_stack.push_back(x);
|
|
}
|
|
}
|
|
|
|
fn pop(&mut self) {
|
|
let val = self.stack.pop_back().unwrap();
|
|
if *self.min_stack.back().unwrap() == val {
|
|
self.min_stack.pop_back();
|
|
}
|
|
}
|
|
|
|
fn top(&self) -> i32 {
|
|
*self.stack.back().unwrap()
|
|
}
|
|
|
|
fn get_min(&self) -> i32 {
|
|
*self.min_stack.back().unwrap()
|
|
}
|
|
}
|