20240303 Finished
This commit is contained in:
parent
08c3712b1d
commit
d71c48ea85
|
@ -61,4 +61,5 @@ mod p2867_count_valid_paths_in_a_tree;
|
|||
mod p2673_make_costs_of_paths_equal_in_a_binary_tree;
|
||||
mod p2581_count_number_of_possible_root_nodes;
|
||||
mod p2369_check_if_there_is_a_valid_partition_for_the_array;
|
||||
mod p2368_reachable_nodes_with_restrictions;
|
||||
mod p2368_reachable_nodes_with_restrictions;
|
||||
mod p225_implement_stack_using_queues;
|
77
src/problem/p225_implement_stack_using_queues.rs
Normal file
77
src/problem/p225_implement_stack_using_queues.rs
Normal file
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* [225] Implement Stack using Queues
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
use std::collections::VecDeque;
|
||||
|
||||
struct MyStack {
|
||||
queue: VecDeque<i32>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl MyStack {
|
||||
|
||||
fn new() -> Self {
|
||||
return MyStack {
|
||||
queue: VecDeque::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, x: i32) {
|
||||
let mut new_queue = VecDeque::with_capacity(self.queue.capacity() + 1);
|
||||
new_queue.push_back(x);
|
||||
|
||||
for i in &self.queue {
|
||||
new_queue.push_back(*i);
|
||||
}
|
||||
|
||||
self.queue = new_queue;
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> i32 {
|
||||
self.queue.pop_front().unwrap()
|
||||
}
|
||||
|
||||
fn top(&self) -> i32 {
|
||||
*self.queue.front().unwrap()
|
||||
}
|
||||
|
||||
fn empty(&self) -> bool {
|
||||
self.queue.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your MyStack object will be instantiated and called as such:
|
||||
* let obj = MyStack::new();
|
||||
* obj.push(x);
|
||||
* let ret_2: i32 = obj.pop();
|
||||
* let ret_3: i32 = obj.top();
|
||||
* let ret_4: bool = obj.empty();
|
||||
*/
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_225() {
|
||||
let mut stack = MyStack::new();
|
||||
|
||||
stack.push(1);
|
||||
stack.push(2);
|
||||
|
||||
assert_eq!(2, stack.top());
|
||||
assert_eq!(2, stack.pop());
|
||||
assert!(!stack.empty());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user