一道简单的设计题,用队列来实现计数,找到索引处的值在t-3000
的临界位置即可。
type RecentCounter struct {
queue []int
}
func Constructor() RecentCounter {
return RecentCounter{
queue : []int{},
}
}
func (this *RecentCounter) Ping(t int) int {
this.queue = append(this.queue, t)
var index int
for this.queue[index] < t-3000 {
index ++
}
return len(this.queue)-index
}
/**
* Your RecentCounter object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Ping(t);
*/