码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Design Hit Counter"

时间:2016-06-28 02:06:28      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

Hits come and go - so we use queue. Nothing special.

class HitCounter {
    queue<int> q;
public:
    /** Initialize your data structure here. */
    HitCounter() {
        
    }
    
    /** Record a hit.
        @param timestamp - The current timestamp (in seconds granularity). */
    void hit(int timestamp) {
        q.push(timestamp);
    }
    
    /** Return the number of hits in the past 5 minutes.
        @param timestamp - The current timestamp (in seconds granularity). */
    int getHits(int timestamp) {
        while(!q.empty() && (timestamp - q.front()) >= 300) q.pop();
        return q.size();
    }
};

LeetCode "Design Hit Counter"

标签:

原文地址:http://www.cnblogs.com/tonix/p/5622032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!