[−][src]Struct leetcode_for_rust::cd0622_design_circular_queue::MyCircularQueue
Solutions
Approach 1:
-
Time complexity: O(1)
-
Space complexity: O(n)
-
Runtime: 4 ms
-
Memory: 2.8 MB
struct MyCircularQueue { items: Vec<Option<i32>>, head: i32, tail: i32, capacity: i32, size: i32, } /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl MyCircularQueue { /** Initialize your data structure here. Set the size of the queue to be k. */ fn new(k: i32) -> Self { MyCircularQueue { items: vec![None; k as usize], head: 0, tail: 0, capacity: k, size: 0, } } /** Insert an element into the circular queue. Return true if the operation is successful. */ fn en_queue(&mut self, value: i32) -> bool { if self.is_full() { return false; } self.items[self.tail as usize] = Some(value); self.tail = (self.tail + 1) % self.capacity; self.size += 1; true } /** Delete an element from the circular queue. Return true if the operation is successful. */ fn de_queue(&mut self) -> bool { if self.is_empty() { return false; } self.items[self.head as usize] = None; self.head = (self.head + 1) % self.capacity; self.size -= 1; true } /** Get the front item from the queue. */ fn front(&self) -> i32 { self.items[self.head as usize].unwrap_or(-1) } /** Get the last item from the queue. */ fn rear(&self) -> i32 { let tmp_tail = if self.tail == 0 { self.capacity - 1 } else { self.tail - 1 }; self.items[tmp_tail as usize].unwrap_or(-1) } /** Checks whether the circular queue is empty or not. */ fn is_empty(&self) -> bool { self.size == 0 } /** Checks whether the circular queue is full or not. */ fn is_full(&self) -> bool { self.size == self.capacity } } /** * Your MyCircularQueue object will be instantiated and called as such: * let obj = MyCircularQueue::new(k); * let ret_1: bool = obj.en_queue(value); * let ret_2: bool = obj.de_queue(); * let ret_3: i32 = obj.front(); * let ret_4: i32 = obj.rear(); * let ret_5: bool = obj.is_empty(); * let ret_6: bool = obj.is_full(); */
Auto Trait Implementations
impl Sync for MyCircularQueue
impl Unpin for MyCircularQueue
impl Send for MyCircularQueue
impl UnwindSafe for MyCircularQueue
impl RefUnwindSafe for MyCircularQueue
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,