362. Design Hit Counter

这个傻逼题。。我没弄明白

you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing)

这句话的意思,以为只是盖戳的时间是这样,getHits可以是任意值,我用TEST CASE测试了一下,答案的结果是这样。

比如300S之后,getHits(4)还是能得到正确解,结果卡了好久。

看了答案,发现似乎getHits的parameter只会比当前时间要晚。。。我真是要报警了。

public class HitCounter {

        int[] hits;
int[] times;
/** Initialize your data structure here. */
public HitCounter()
{
hits = new int[300];
times = new int[300];
} /** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp)
{
if(times[timestamp%300] == timestamp)
{
hits[timestamp%300]++;
}
else
{
hits[timestamp%300] = 1;
times[timestamp%300] = timestamp;
}
} /** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp)
{
int res = 0;
for(int i = 0; i < 300;i++)
{
if(timestamp - times[i] < 300) res += hits[i];
} return res;
}
}

我这个智商没救了,别刷题了,去吃屎好了。

上一篇:Cocos2dx-3.0版本 从开发环境搭建(Win32)到项目移植Android平台过程详解


下一篇:LeetCode Design TinyURL