-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdesign-front-middle-back-queue.rs
96 lines (84 loc) · 2.52 KB
/
design-front-middle-back-queue.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
struct FrontMiddleBackQueue {
pre: std::collections::LinkedList<i32>,
back: std::collections::LinkedList<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
/**
* Your FrontMiddleBackQueue object will be instantiated and called as such:
* let obj = FrontMiddleBackQueue::new();
* obj.push_front(val);
* obj.push_middle(val);
* obj.push_back(val);
* let ret_4: i32 = obj.pop_front();
* let ret_5: i32 = obj.pop_middle();
* let ret_6: i32 = obj.pop_back();
*/
impl FrontMiddleBackQueue {
fn new() -> Self {
Self {
pre: std::collections::LinkedList::new(),
back: std::collections::LinkedList::new(),
}
}
fn push_front(&mut self, val: i32) {
if self.pre.len() > self.back.len() {
if let Some(x) = self.pre.pop_back() {
self.back.push_front(x);
}
}
self.pre.push_front(val);
}
fn push_middle(&mut self, val: i32) {
match self.pre.len() - self.back.len() {
1 => {
if let Some(x) = self.pre.pop_back() {
self.back.push_front(x);
}
self.pre.push_back(val);
}
_ => self.pre.push_back(val),
}
}
fn push_back(&mut self, val: i32) {
self.back.push_back(val);
if self.back.len() > self.pre.len() {
if let Some(x) = self.back.pop_front() {
self.pre.push_back(x);
}
}
}
fn pop_front(&mut self) -> i32 {
if self.pre.len() == self.back.len() {
if let Some(x) = self.back.pop_front() {
self.pre.push_back(x);
}
}
self.pre.pop_front().unwrap_or(-1)
}
fn pop_middle(&mut self) -> i32 {
match self.pre.len() - self.back.len() {
1 => self.pre.pop_back().unwrap_or(-1),
_ => {
let val = self.pre.pop_back().unwrap_or(-1);
if let Some(x) = self.back.pop_front() {
self.pre.push_back(x);
}
val
}
}
}
fn pop_back(&mut self) -> i32 {
if self.pre.len() - self.back.len() > 0 {
if let Some(x) = self.pre.pop_back() {
self.back.push_front(x);
}
}
self.back.pop_back().unwrap_or(-1)
}
}