【五月集训5.16】———队列

请添加图片描述

☘前言☘

开更五月集训专题,由浅入深,深入浅出,飞向大厂!

🧑🏻作者简介:一个从工业设计改行学嵌入式的年轻人
✨联系方式:2201891280(QQ)
全文大约阅读时间: 20min



933. 最近的请求次数

933. 最近的请求次数

解题思路

每次入队之后进行判断是否需要删除元素。

代码

class RecentCounter {
    vector<int> count;
    int start;
public:
    RecentCounter() {
        count.resize(0);
        start = 0;
    }
    
    int ping(int t) {
        count.push_back(t);
        while(t - count[start] > 3000)  ++start;
        return count.size() - start;
    }
};

注意的点

因为使用的是顺序表,所以删除的效率过低,所以这个地方采用的是标记删除的方法。


2073. 买票需要的时间

2073. 买票需要的时间

解题思路

在位置之前的位置最多就是买的和所给位置相同,在位置之后的最多买所给位置-1的票。

代码

class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int num = tickets[k],n = tickets.size(),ans = 0;
        for(int i = 0 ;i < n;++i)
            if(i <= k)  ans += tickets[i] > num ? num : tickets[i];
            else ans += tickets[i] > (num - 1) ? num - 1 : tickets[i];
        return ans;
    }
};

注意的点

  1. 0需要特判,但是这题给的范围没有,所以就不用了。主要是num不能是0。

641. 设计循环双端队列

641. 设计循环双端队列

解题思路

用数组模拟就完事了。

代码

class MyCircularDeque {
    vector<int> duilie;
    int size, tail, head;
public:
    MyCircularDeque(int k) {
        duilie.resize(k+1);//初始化
        size = k + 1;
        head = tail = 0;
    }
    
    bool insertFront(int value) {
        if(isFull())    return false;
        duilie[head] = value;
        head = (size + head - 1) % size; //注意
        return true;
    }
    
    bool insertLast(int value) {
        if(isFull())    return false;
        tail = (tail + 1) % size;
        duilie[tail] = value;
        return true;
    }
    
    bool deleteFront() {
        if(isEmpty())   return false;
        head = (head + 1) % size;
        return true;
    }
    
    bool deleteLast() {
        if(isEmpty())   return false;
        tail = (size + tail - 1) % size;	//注意
        return true;
    }
    
    int getFront() {
        if(isEmpty())   return -1;
        return duilie[(head + 1) % size];
    }
    
    int getRear() {
        if(isEmpty())   return -1;
        return duilie[tail];
    }
    
    bool isEmpty() {
        if(head == tail)    return true;
        return false;
    }
    
    bool isFull() {
        if((tail + 1) % size == head)   return true;
        return false;
    }
};

注意的点

  1. 注册数组大小要比真是大小大1,否则不好区分满和空的情况
  2. -1的时候要注意加一个size否则会出现负数

1670. 设计前中后队列

1670. 设计前中后队列

解题思路

直接使用数组模拟,反正数据量不大。

代码

class FrontMiddleBackQueue {
    vector<int> duilie;
    int front,rear;
    bool isEmpty(){
        if(front == rear)   return true;
        return false;
    }
public:
    FrontMiddleBackQueue() {
        duilie.resize(2000);
        front = rear = 1000;
    }
    
    void pushFront(int val) {
        duilie[front--] = val;
    }
    
    void pushMiddle(int val) {
        duilie.insert(duilie.begin() + (front + rear)/2 + 1, val);
        ++rear;
    }
    
    void pushBack(int val) {
        duilie[++rear] = val;
    }
    
    int popFront() {
        if(isEmpty())   return -1;
        int tmp = duilie[front + 1];
        duilie.erase(duilie.begin() + front + 1);
        --rear;
        return tmp;
    }
    
    int popMiddle() {
        if(isEmpty())   return -1;
        int tmp = duilie[(front + rear + 1) /2 ];
        duilie.erase(duilie.begin() + (front + rear + 1) /2);
        --rear;
        return tmp;
    }
    
    int popBack() {
        if(isEmpty())   return -1;
        int tmp = duilie[rear];
        duilie.erase(duilie.begin() + rear);
        --rear;
        return tmp;
    }
};

注意的点

  1. 插入的位置和删除的middle位置不一样
  2. 直接用insert和erase会简单不少

写在最后

今天又进入简单模式0.0

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XingleiGao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值