id 1414830744 Time: 18 ms MemUsage: 6.3 MB
This commit is contained in:
36
leetcode/number-of-recent-calls/solution_1414830744.rs
Normal file
36
leetcode/number-of-recent-calls/solution_1414830744.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
struct RecentCounter {
|
||||
times: 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 {
|
||||
Self{
|
||||
times: VecDeque::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn ping(&mut self, t: i32) -> i32 {
|
||||
while let Some(x) = self.times.pop_front(){
|
||||
if t - x <= 3000 {
|
||||
self.times.push_front(x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
self.times.push_back(t);
|
||||
self.times.len() as i32
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your RecentCounter object will be instantiated and called as such:
|
||||
* let obj = RecentCounter::new();
|
||||
* let ret_1: i32 = obj.ping(t);
|
||||
*/
|
||||
Reference in New Issue
Block a user