标题: Leetcode - Design Hit Counter(2) [打印本页] 作者: look_w 时间: 2019-2-19 15:00 标题: Leetcode - Design Hit Counter(2)
然后看答案看到了一种新的方法:
My code:
public class HitCounter {
Queue<Integer> q = new LinkedList<Integer>();
/** Initialize your data structure here. */
public HitCounter() {
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
q.offer(timestamp);
}
/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp) {
while (!q.isEmpty() && timestamp - q.peek() >= 300) {
q.poll();
}
return q.size();
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/