mirror of https://github.com/doocs/leetcode.git
42 lines
785 B
Rust
42 lines
785 B
Rust
use std::collections::VecDeque;
|
|
|
|
struct MyQueue {
|
|
stk1: Vec<i32>,
|
|
stk2: Vec<i32>,
|
|
}
|
|
|
|
impl MyQueue {
|
|
fn new() -> Self {
|
|
MyQueue {
|
|
stk1: Vec::new(),
|
|
stk2: Vec::new(),
|
|
}
|
|
}
|
|
|
|
fn push(&mut self, x: i32) {
|
|
self.stk1.push(x);
|
|
}
|
|
|
|
fn pop(&mut self) -> i32 {
|
|
self.move_elements();
|
|
self.stk2.pop().unwrap()
|
|
}
|
|
|
|
fn peek(&mut self) -> i32 {
|
|
self.move_elements();
|
|
*self.stk2.last().unwrap()
|
|
}
|
|
|
|
fn empty(&self) -> bool {
|
|
self.stk1.is_empty() && self.stk2.is_empty()
|
|
}
|
|
|
|
fn move_elements(&mut self) {
|
|
if self.stk2.is_empty() {
|
|
while let Some(element) = self.stk1.pop() {
|
|
self.stk2.push(element);
|
|
}
|
|
}
|
|
}
|
|
}
|