Un Leetcode por día: 933. El número de solicitudes recientes [cola]

Inserte la descripción de la imagen aquí
// Principalmente porque el significado del título no es fácil de entender

class RecentCounter {
    
    
    // 定义队列
    Queue<Integer> q;
    public RecentCounter() {
    
    
        q = new LinkedList();
    }
    
    public int ping(int t) {
    
    
        q.add(t);
        // 判断t-3000的起始位置,不满足条件的弹出
        while(q.peek()<t-3000)
            q.poll();
        // 满足条件的直接计算队列长度即可
        return q.size();
    }
}

/**
 * Your RecentCounter object will be instantiated and called as such:
 * RecentCounter obj = new RecentCounter();
 * int param_1 = obj.ping(t);
 */

Supongo que te gusta

Origin blog.csdn.net/weixin_41041275/article/details/111715154
Recomendado
Clasificación