Rust Solution:
struct MyQueue { stack1: Vec<i32>, stack2: Vec<i32>, } impl MyQueue { pub fn new() -> Self { MyQueue { stack1: vec![], stack2: vec![], } } pub fn push(&mut self, x: i32) { self.stack1.push(x); } pub fn pop(&mut self) { if self.stack2.is_empty() { while !self.stack1.is_empty() { self.stack2.push(self.stack1.pop().unwrap()); } } self.stack2.pop(); } pub fn peek(&mut self) -> i32 { if self.stack2.is_empty() { while !self.stack1.is_empty() { self.stack2.push(self.stack1.pop().unwrap()); } } *self.stack2.last().unwrap() } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust Solution: