[][src]Struct leetcode_for_rust::cd0933_number_of_recent_calls::RecentCounter

pub struct RecentCounter {}

Solutions

Approach 1: Queue

  • Time complexity: O(n)

  • Space complexity: O(1)

  • Runtime: 40 ms

  • Memory: 5.4 MB

use std::collections::VecDeque;

#[derive(Default)]
struct RecentCounter {
    pings: VecDeque<i32>,
}

/**
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl RecentCounter {

    fn new() -> Self {
       Default::default()
    }

    fn ping(&mut self, t: i32) -> i32 {
       self.pings.push_back(t);
        while t - *self.pings.front().unwrap() > 3000 {
            self.pings.pop_front();
        }

        self.pings.len() as i32
    }
}

/**
 * Your RecentCounter object will be instantiated and called as such:
 * let obj = RecentCounter::new();
 * let ret_1: i32 = obj.ping(t);
 */

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]