zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    

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

  • 相关阅读:
    开发细节
    html
    java学习
    Promise对象
    强制转换和隐式转换
    借助防抖解决输入框的非空校验
    setTimeout
    Symbol类型
    js API
    vue 使用mixin
  • 原文地址:https://www.cnblogs.com/reboot329/p/5935898.html
Copyright © 2011-2022 走看看