|
| 1 | +/** |
| 2 | + * [232] Implement Queue using Stacks |
| 3 | + * |
| 4 | + * Implement the following operations of a queue using stacks. |
| 5 | + * |
| 6 | + * |
| 7 | + * push(x) -- Push element x to the back of queue. |
| 8 | + * pop() -- Removes the element from in front of queue. |
| 9 | + * peek() -- Get the front element. |
| 10 | + * empty() -- Return whether the queue is empty. |
| 11 | + * |
| 12 | + * |
| 13 | + * Example: |
| 14 | + * |
| 15 | + * |
| 16 | + * MyQueue queue = new MyQueue(); |
| 17 | + * |
| 18 | + * queue.push(1); |
| 19 | + * queue.push(2); |
| 20 | + * queue.peek(); // returns 1 |
| 21 | + * queue.pop(); // returns 1 |
| 22 | + * queue.empty(); // returns false |
| 23 | + * |
| 24 | + * Notes: |
| 25 | + * |
| 26 | + * |
| 27 | + * You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid. |
| 28 | + * Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack. |
| 29 | + * You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue). |
| 30 | + * |
| 31 | + * |
| 32 | + */ |
| 33 | +pub struct Solution {} |
| 34 | + |
| 35 | +// submission codes start here |
| 36 | + |
| 37 | +struct MyQueue { |
| 38 | + vec1: Vec<i32>, |
| 39 | + vec2: Vec<i32>, |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * `&self` means the method takes an immutable reference. |
| 44 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 45 | + */ |
| 46 | +impl MyQueue { |
| 47 | + /** Initialize your data structure here. */ |
| 48 | + fn new() -> Self { |
| 49 | + MyQueue{ |
| 50 | + vec1: Vec::new(), |
| 51 | + vec2: Vec::new(), |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** Push element x to the back of queue. */ |
| 56 | + fn push(&mut self, x: i32) { |
| 57 | + while let Some(v) = self.vec1.pop() { |
| 58 | + self.vec2.push(v); |
| 59 | + } |
| 60 | + self.vec2.push(x); |
| 61 | + while let Some(v) = self.vec2.pop() { |
| 62 | + self.vec1.push(v); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** Removes the element from in front of queue and returns that element. */ |
| 67 | + fn pop(&mut self) -> i32 { |
| 68 | + self.vec1.pop().unwrap() |
| 69 | + } |
| 70 | + |
| 71 | + /** Get the front element. */ |
| 72 | + fn peek(&self) -> i32 { |
| 73 | + *self.vec1.last().unwrap() |
| 74 | + } |
| 75 | + |
| 76 | + /** Returns whether the queue is empty. */ |
| 77 | + fn empty(&self) -> bool { |
| 78 | + self.vec1.is_empty() |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Your MyQueue object will be instantiated and called as such: |
| 84 | + * let obj = MyQueue::new(); |
| 85 | + * obj.push(x); |
| 86 | + * let ret_2: i32 = obj.pop(); |
| 87 | + * let ret_3: i32 = obj.peek(); |
| 88 | + * let ret_4: bool = obj.empty(); |
| 89 | + */ |
| 90 | + |
| 91 | +// submission codes end |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod tests { |
| 95 | + use super::*; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_232() { |
| 99 | + let mut queue = MyQueue::new(); |
| 100 | + |
| 101 | + queue.push(1); |
| 102 | + queue.push(2); |
| 103 | + assert_eq!(queue.peek(), 1); // returns 1 |
| 104 | + assert_eq!(queue.pop(), 1); // returns 1 |
| 105 | + assert_eq!(queue.empty(), false); // returns false |
| 106 | + } |
| 107 | +} |
0 commit comments