1 Public classHitCounter {2 Private int[] count;3 Private int[] indexes;4 5 /**Initialize your data structure here.*/6 PublicHitCounter () {7Count =New int[300];8indexes =New int[300];9 for(inti = 0; I < 300; i++) {TenIndexes[i] =i; One } A } - - /**Record a hit. the @paramtimestamp-the current timestamp (in seconds granularity).*/ - Public voidHitinttimestamp) { - intindex = timestamp% 300; - if(Timestamp! =Indexes[index]) { +Indexes[index] =timestamp; -Count[index] = 0; + } Acount[index]++; at } - - /**Return the number of hits in the past 5 minutes. - @paramtimestamp-the current timestamp (in seconds granularity).*/ - Public intGethits (inttimestamp) { - intresult = 0; in for(inti = 0; I < 300; i++) { -Result + = Timestamp-indexes[i] < 300? Count[i]: 0; to } + returnresult; - } the } * $ /**Panax Notoginseng * Your HitCounter object would be instantiated and called as such: - * HitCounter obj = new HitCounter (); the * Obj.hit (timestamp); + * int param_2 = obj.gethits (timestamp); A */
1. Included
Design hit Counter