LeetCode 641: Design Circular Deque

KiCad 7.0 + 嘉立创打样:从原理图到实物,打造你的双LLCC68 LoRa串口透传板 本文详细介绍了使用KiCad 7.0设计双LLCC68 LoRa串口透传板的完整流程,从原理图设计到嘉立创打样。重点解析了AVR+LLCC68组合的优势、双模块布局的电磁兼容设计、SPI接口配置及射频走线技巧,并提供了实测数据和常见问题解决方案,帮助开发者高效实现物联网设备的低功耗远距离通信。 阅读详情
  1. Design Circular Deque
    Medium
    1K
    71
    Companies
    Design your implementation of the circular double-ended queue (deque).

Implement the MyCircularDeque class:

MyCircularDeque(int k) Initializes the deque with a maximum size of k.
boolean insertFront() Adds an item at the front of Deque. Returns true if the operation is successful, or false otherwise.
boolean insertLast() Adds an item at the rear of Deque. Returns true if the operation is successful, or false otherwise.
boolean deleteFront() Deletes an item from the front of Deque. Returns true if the operation is successful, or false otherwise.
boolean deleteLast() Deletes an item from the rear of Deque. Returns true if the operation is successful, or false otherwise.
int getFront() Returns the front item from the Deque. Returns -1 if the deque is empty.
int getRear() Returns the last item from Deque. Returns -1 if the deque is empty.
boolean isEmpty() Returns true if the deque is empty, or false otherwise.
boolean isFull() Returns true if the deque is full, or false otherwise.

Example 1:

Input
[“MyCircularDeque”, “insertLast”, “insertLast”, “insertFront”, “insertFront”, “getRear”, “isFull”, “deleteLast”, “insertFront”, “getFront”]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
Output
[null, true, true, true, false, 2, true, true, true, 4]

Explanation
MyCircularDeque myCircularDeque = new MyCircularDeque(3);
myCircularDeque.insertLast(1); // return True
myCircularDeque.insertLast(2); // return True
myCircularDeque.insertFront(3); // return True
myCircularDeque.insertFront(4); // return False, the queue is full.
myCircularDeque.getRear(); // return 2
myCircularDeque.isFull(); // return True
myCircularDeque.deleteLast(); // return True
myCircularDeque.insertFront(4); // return True
myCircularDeque.getFront(); // return 4

Constraints:

1 <= k <= 1000
0 <= value <= 1000
At most 2000 calls will be made to insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull.

解法1:用vector或数组
注意front和rear一开始都是-1,当进行第一次insertFront()或insertRear()之后,它们的值要变成0。否则后面的getRear()或getFront()会出错。

class MyCircularDeque {
public:
    MyCircularDeque(int k) {
        data.resize(k, 0);
        capacity = k;
        count = 0;
        front = -1;
        rear = -1;
    }
    
    bool insertFront(int value) {
        if (count >= capacity) return false;
        if (front == -1 && rear == -1) { //the first insert
            front = rear = 0;
        } else {
            front = (front - 1 + capacity) % capacity;
        }
        data[front] = value;
        count++;
        return true;
    }
    
    bool insertLast(int value) {
        if (count >= capacity) return false;
        if (front == -1 && rear == -1) { //the first insert
            front = rear = 0;
        } else {
            rear = (rear + 1) % capacity;
        }
        data[rear] = value;
        count++;
        return true;
    }
    
    bool deleteFront() {
        if (count > 0) {
            front = (front + 1) % capacity;
            count--;
            return true;
        }
        return false;
    }
    
    bool deleteLast() {
        if (count > 0) {
            rear = (rear - 1 + capacity) % capacity;
            count--;
            return true;
        }
        return false;
    }
    
    int getFront() {
        if (count == 0) return -1;
        return data[front];
    }
    
    int getRear() {
        if (count == 0) return -1;
        return data[rear];
    }
    
    bool isEmpty() {
        return count == 0;
    }
    
    bool isFull() {
        return count == capacity;        
    }
private:
    vector<int> data;
    int capacity, count, front, rear;
};

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque* obj = new MyCircularDeque(k);
 * bool param_1 = obj->insertFront(value);
 * bool param_2 = obj->insertLast(value);
 * bool param_3 = obj->deleteFront();
 * bool param_4 = obj->deleteLast();
 * int param_5 = obj->getFront();
 * int param_6 = obj->getRear();
 * bool param_7 = obj->isEmpty();
 * bool param_8 = obj->isFull();
 */
Vue3项目实战:CKEditor5富文本编辑器从安装到隐藏不需要的功能 本文详细介绍了在Vue3项目中集成并深度定制CKEditor5富文本编辑器的完整流程。通过分析其模块化架构,指导开发者从标准构建出发,移除图片、表格等冗余插件,创建专属轻量构建,从而显著减小打包体积并提升用户体验。文章结合实战代码,涵盖了环境搭建、自定义构建配置、高级优化技巧等关键步骤。 阅读详情

相关推荐

[misc]智能车协议分析:2020网鼎杯青龙组misc_teslaaaaa_wp

[misc]智能车协议分析:2020网鼎杯青龙组misc_teslaaaaa_wp 文章目录[misc]智能车协议分析:2020网鼎杯青龙组misc_teslaaaaa_wp分析log文件格式分析协议组成UDS网络传输层UDS统一诊断服务协议分析文件提取文件逆向flag计算参考链接: teslaaaaa这道题是青龙组被做出来的题目中做出来人数最少的,直到比赛快结束才有第一个人做出来,怎么说,难并不是因为脑洞大,讲道理这道题是一道好题,我当时也是肝了一天没做出来,我倒是把文件提取出来了,奈何比赛时间已经接近尾

Breeze的博客 1万+

java_力扣_双端队列

力扣641:https://leetcode.cn/problems/design-circular-deque/题目:循环双端队列(使用循环数组实现)

2303_78983004的博客 278

深入解析IC测试向量(Pattern)转换技术及其应用实践

本文深入解析了IC测试向量(Pattern)转换技术,重点阐述了如何将设计仿真生成的VCD文件转换为ATE机台可执行的STIL格式。文章详细拆解了转换的核心步骤、常见坑点,并对比了Vtran、ATEgen等主流工具,为芯片测试工程师提供了从原理到实战的完整指南,旨在提升测试效率与可靠性。

weixin_29281941的博客 602

LeetCode:641. Design Circular Deque设计循环双端队列(C语言)

题目描述: 设计实现双端队列。 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k。 insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。 insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。 deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。 deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。 getFront():从双端队列头

C/C++攻城狮 329

ESXI 8.0.3 ubuntu 24.04 直通显卡P40

手把手,一步步教你直通GPU,ESXI 8.0.3 ubuntu 24.04 直通显卡P40

qq_36433118的博客 4692

2024年CSP-X复赛真题题解(T1:购物)

2024年CSP-X复赛真题题解(T1:购物)

王老师青少年编程 1189

LeetCode //C - 641. Design Circular Deque

【代码】LeetCode //C - 641. Design Circular Deque。

Made in Code 1938

边缘计算与产业生态系统

目录 文章目录目录边缘计算产业参与者边缘计算产业生态系统 边缘计算产业参与者 边缘计算领域参与者众多,主流公司以打造生态优势为目标,基于自身服务层级向产业链上下两端进行拓展,目前市场主要参与者有: 系统集成商:在产业头部,具有广泛的能力,可以集成不同生态系统公司的产品,具有一定的全球性属性,在边缘计算领域大多由云服务商、电信运营商担任。 云服务商:具有领先的云技术优势与应用生态体系,同时拥有全球性资源布局与市场资源生态,并渴望成为边缘计算主导者。 电信运营商:具有边缘基础设施优势,以及不可替代的

烟云的计算 2110

641. Design Circular Deque [LeetCode]

英文网址 中文网址 我的解答20190412 class MyCircularDeque(object): def __init__(self, k): """ Initialize your data structure here. Set the size of the deque to be k. :type k: int ...

小白夺植物战僵尸的博客 196

EtherCAT主站教程3--适配伺服驱动器(模式配置)

目前通用的伺服驱动器的位置模式、速度模式以及其他模式是伺服系统中常见的控制模式,切换这些模式通常需要根据具体的伺服驱动器型号和控制协议(如EtherCAT、CANopen、Modbus等)进行配置。以下是这些模式的概述及切换方法的一般性指导:协议规范中,一共有以下几种模式:其中模式1、3、6对于比较常用,也更容易上手。

weixin_52626887的博客 800

国网DLT698.45协议——采集系统、数据交换(一)

对于国网698协议,是一种面向对象的通信协议,用于远程监控和控制电力系统中的设备。面向对象使得对协议的思考更趋向于正常思维,使计算机中描述的抽象世界于现实世界中能够更好的对应起来。协议中就是将各种方法和属性对应描述成类和对象。接下来是我学习该协议的一些总结和认识,欢迎大家随时在评论区或者私信我交流学习。:系统=对象+消息:程序=算法+数据对象和接口类描述:类描述了一组具有相同属性和相同方法的对象。

一个bug是bug,多个bug能work 1万+

【Leetcode】641. Design Circular Deque

题目地址: https://leetcode.com/problems/design-circular-deque/ 设计循环双端队列。初始化时会给定一个capacity,不需要扩容。当添加元素的时候,如果队列满,则返回false,否则添加并返回true;当查看元素时,如果队列空则返回−1-1−1,否则返回查看所得值;当删除元素时,如果队列空则返回false,否则删除并返回true。 用一个数组表示队列,并用两个指针分别指向队列头和队列尾,再用一个变量记录队列里元素个数。每当添加的时候都需要注意判断当前队列

数学、算法爱好者的博客 269

【PINN物理信息网络】基于PINN物理信息网络求解泊松方程(python)

【PINN物理信息网络】基于PINN物理信息网络求解泊松方程(python)

k8291121的博客 2818

[leetcode]641. Design Circular Deque

[leetcode]641. Design Circular Deque Analysis 年纪大了换季就会感冒—— [ummmm~] Design your implementation of the circular double-ended queue (deque). Your implementation should support following opera...

weixin_32135877的博客 393

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

[Leetcode]_641 Design Circular Deque

/** * Index: 641 * Title: Design Circular Deque * Author: ltree98 **/ 题意 设计一个环形队列 支持的操作 MyCircularDeque(k): 构造方法,设置队列长度k insertFront(): 插入元素到队头,返回值表示操作是否成功 insertLast(): 插入元素到队尾,返回值表示操作是否...

继续激情,继续奋斗。 376

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode 641. Design Circular Deque

题目描述 Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: MyCircularDeque(k): Constructor, set the size of the deque to be ...

weixin_40275300的博客 174

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LeetCode(Queue)641. Design Circular Deque

LeetCode(Queue)641. Design Circular Deque

weixin_45428910的博客 236

leetcode雕虫小技medium 641. Design Circular Deque

题干:https://leetcode.com/problems/design-circular-deque/ 这题跟622类似,算是它的进阶版,要支持双向的queue,大部分代码我都复用622的方案, 纬度要注意的一点是deleteLast和deleteFront要注意边界条件,就是当删完之后队列为空时,不要移动currentHead或者currentTail指针位置。 代码如下: ...

xunileida的专栏 212

LeetCode_Queue_641. Design Circular Deque 设计循环双端队列(C++)

目录 一,题目描述 英文描述 中文描述 二,解题思路 三,AC代码 C++ 四,解题过程 第一博 一,题目描述 英文描述 Design your implementation of the circular double-ended queue (deque). Implement the MyCircularDeque class: MyCircularDeque(int k) Initializes the deque with a maximum size of k....

&再见萤火虫&的博客 262

LeetCode 之 JavaScript 解答第641题 —— 设计双端队列(Design Circular Deque)

Time:2019/4/15 Title: Design Circular Deque Difficulty: Medium Author: 小鹿 题目:Design Circular Deque Design your implementation of the circular double-ended queue (deque). Your implementation should s...

小鹿动画学编程 431

leetcode 641. Design Circular Deque 设计循环双端队列

本文实现了一个固定大小的循环双端队列(Circular Deque)。使用数组存储元素,并通过front和back指针分别记录队列头部和尾部位置,num变量统计当前元素数量。主要操作包括:头部/尾部插入元素(insertFront/insertLast)、头部/尾部删除元素(deleteFront/deleteLast)、获取头部/尾部元素(getFront/getRear),以及判断队列是否为空或已满(isEmpty/isFull)。所有操作时间复杂度均为O(1),通过模运算处理指针循环移动。该实现满足循

邹九的个人博客 193

Leetcode 641.设计循环双端队列(Design Circular Deque)

Leetcode 641.设计循环双端队列 1 题目描述(Leetcode题目链接)   设计实现双端队列。 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k。 insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。 insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。 deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。 deleteLast():从双端队列尾

qq_39378221的博客 216

LeetCode C++ 641. Design Circular Deque【设计/队列】中等

Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: MyCircularDeque(k) : Constructor, set the size of the deque to be k . insertFront() : Adds an item at the front of Deque. Retur

memcpy0的博客 389

Leetcode641.Design Circular Deque设计循环双端队列

设计实现双端队列。 你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k。 insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。 insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。 deleteFront():从双端队列头部删除一个元...

Ha12312的博客 68

Leetcode - 641 - Design Circular Deque (medium)

Solutions: C++: #include <vector> #include <iostream> using namespace std; class MyCircularDeque { private: vector<int> buffer; int cnt; int k; int front; int rear; public: /** Initialize your data structure..

xin_rong的博客 158
上一篇: Linux内核学习小结
下一篇: BLE 学习小结
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值