数据结构与算法之栈: LeetCode 641. 设计循环双端队列 (Ts版)

news2025/2/3 12:55:23

设计循环双端队列

  • https://leetcode.cn/problems/design-circular-deque/description/

描述

  • 设计实现双端队列。

  • 实现 MyCircularDeque 类:

    • MyCircularDeque(int k) :构造函数,双端队列最大为 k 。
    • boolean insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true ,否则返回 false 。
    • boolean insertLast() :将一个元素添加到双端队列尾部。如果操作成功返回 true ,否则返回 false 。
    • boolean deleteFront() :从双端队列头部删除一个元素。 如果操作成功返回 true ,否则返回 false 。
    • boolean deleteLast() :从双端队列尾部删除一个元素。如果操作成功返回 true ,否则返回 false 。
    • int getFront() ):从双端队列头部获得一个元素。如果双端队列为空,返回 -1 。
    • int getRear() :获得双端队列的最后一个元素。 如果双端队列为空,返回 -1 。
    • boolean isEmpty() :若双端队列为空,则返回 true ,否则返回 false 。
    • boolean isFull() :若双端队列满了,则返回 true ,否则返回 false 。

示例 1:

输入
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
输出
[null, true, true, true, false, 2, true, true, true, 4]

解释

MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
circularDeque.insertLast(1);			        // 返回 true
circularDeque.insertLast(2);			        // 返回 true
circularDeque.insertFront(3);			        // 返回 true
circularDeque.insertFront(4);			        // 已经满了,返回 false
circularDeque.getRear();  				// 返回 2
circularDeque.isFull();				        // 返回 true
circularDeque.deleteLast();			        // 返回 true
circularDeque.insertFront(4);			        // 返回 true
circularDeque.getFront();				// 返回 4

提示:

  • 1 <= k <= 1000
  • 0 <= value <= 1000
  • insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000 次

Typescript 版算法实现


1 ) 方案1:数组

class MyCircularDeque {
    private capacity: number;
    private rear: number;
    private front: number;
    private elements: number[];

    constructor(k: number) {
        this.capacity = k + 1; // 使用 k+1 来区分满和空的情况
        this.rear = 0;
        this.front = 0;
        this.elements = new Array<number>(this.capacity).fill(0);
    }

    insertFront(value: number): boolean {
        if (this.isFull()) {
            return false;
        }
        this.front = (this.front - 1 + this.capacity) % this.capacity;
        this.elements[this.front] = value;
        return true;
    }

    insertLast(value: number): boolean {
        if (this.isFull()) {
            return false;
        }
        this.elements[this.rear] = value;
        this.rear = (this.rear + 1) % this.capacity;
        return true;
    }

    deleteFront(): boolean {
        if (this.isEmpty()) {
            return false;
        }
        this.front = (this.front + 1) % this.capacity;
        return true;
    }

    deleteLast(): boolean {
        if (this.isEmpty()) {
            return false;
        }
        this.rear = (this.rear - 1 + this.capacity) % this.capacity;
        return true;
    }

    getFront(): number {
        if (this.isEmpty()) {
            return -1;
        }
        return this.elements[this.front];
    }

    getRear(): number {
        if (this.isEmpty()) {
            return -1;
        }
        return this.elements[(this.rear - 1 + this.capacity) % this.capacity];
    }

    isEmpty(): boolean {
        return this.rear === this.front;
    }

    isFull(): boolean {
        return (this.rear + 1) % this.capacity === this.front;
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * var obj = new MyCircularDeque(k)
 * var param_1 = obj.insertFront(value)
 * var param_2 = obj.insertLast(value)
 * var param_3 = obj.deleteFront()
 * var param_4 = obj.deleteLast()
 * var param_5 = obj.getFront()
 * var param_6 = obj.getRear()
 * var param_7 = obj.isEmpty()
 * var param_8 = obj.isFull()
 */

2 ) 方案2:链表

// 定义节点结构
class Node {
    prev: Node | null = null;
    next: Node | null = null;
    val: number;

    constructor(value: number) {
        this.val = value;
    }
}

class MyCircularDeque {
    head: Node | null = null;
    tail: Node | null = null;
    capacity: number;
    size: number = 0;

    constructor(k: number) {
        this.capacity = k;
    }

    // 在队列前端插入元素
    insertFront(value: number): boolean {
        if (this.isFull()) {
            return false;
        }
        const newNode = new Node(value);
        if (this.isEmpty()) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            newNode.next = this.head;
            if (this.head) {
                this.head.prev = newNode;
            }
            this.head = newNode;
        }
        this.size++;
        return true;
    }

    // 在队列末端插入元素
    insertLast(value: number): boolean {
        if (this.isFull()) {
            return false;
        }
        const newNode = new Node(value);
        if (this.isEmpty()) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            if (this.tail) {
                this.tail.next = newNode;
                newNode.prev = this.tail;
            }
            this.tail = newNode;
        }
        this.size++;
        return true;
    }

    // 从队列前端删除元素
    deleteFront(): boolean {
        if (this.isEmpty()) {
            return false;
        }
        if (this.head) {
            this.head = this.head.next;
            if (this.head) {
                this.head.prev = null;
            } else {
                this.tail = null; // 如果删除后队列为空,更新尾指针
            }
        }
        this.size--;
        return true;
    }

    // 从队列末端删除元素
    deleteLast(): boolean {
        if (this.isEmpty()) {
            return false;
        }
        if (this.tail) {
            this.tail = this.tail.prev;
            if (this.tail) {
                this.tail.next = null;
            } else {
                this.head = null; // 如果删除后队列为空,更新头指针
            }
        }
        this.size--;
        return true;
    }

    // 获取队列前端的元素
    getFront(): number {
        if (this.isEmpty()) {
            return -1;
        }
        return this.head?.val ?? -1;
    }

    // 获取队列末端的元素
    getRear(): number {
        if (this.isEmpty()) {
            return -1;
        }
        return this.tail?.val ?? -1;
    }

    // 检查队列是否为空
    isEmpty(): boolean {
        return this.size === 0;
    }

    // 检查队列是否已满
    isFull(): boolean {
        return this.size === this.capacity;
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * var obj = new MyCircularDeque(k)
 * var param_1 = obj.insertFront(value)
 * var param_2 = obj.insertLast(value)
 * var param_3 = obj.deleteFront()
 * var param_4 = obj.deleteLast()
 * var param_5 = obj.getFront()
 * var param_6 = obj.getRear()
 * var param_7 = obj.isEmpty()
 * var param_8 = obj.isFull()
 */

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2291275.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《基于Scapy的综合性网络扫描与通信工具集解析》

在网络管理和安全评估中&#xff0c;网络扫描和通信是两个至关重要的环节。Python 的 Scapy 库因其强大的网络数据包处理能力&#xff0c;成为开发和实现这些功能的理想工具。本文将介绍一个基于 Scapy 编写的 Python 脚本&#xff0c;该脚本集成了 ARP 扫描、端口扫描以及 TCP…

基于Python的药物相互作用预测模型AI构建与优化(上.文字部分)

一、引言 1.1 研究背景与意义 在临床用药过程中,药物相互作用(Drug - Drug Interaction, DDI)是一个不可忽视的重要问题。当患者同时服用两种或两种以上药物时,药物之间可能会发生相互作用,从而改变药物的疗效、增加不良反应的发生风险,甚至危及患者的生命安全。例如,…

Linux环境下的Java项目部署技巧:环境安装

安装 JDK&#xff1a; 第上传 jdk 压缩安装包到服务器 将压缩安装包解压缩&#xff1a; tar -xvf jdk-8uXXX-linux-x64.tar.gz 配置环境变量&#xff1a; 编辑 /etc/profile 文件&#xff0c;在文件末尾添加以下内容&#xff1a; export JAVA_HOME/path/to/jdk //JAVA_HOME…

【系统迁移】将系统迁移到新硬盘中(G15 5520)

文章目录 前言问题描述解决步骤&#xff08;红色为 debug 步骤&#xff09;参考文献 前言 参数&#xff1a; 电脑 dell g15 5520硬盘&#xff1a;1T 自带硬盘 海力士 2230 -> 2T 西数蓝盘 2280 问题描述 电脑硬盘过小&#xff08;且只有一个接口&#xff09;&#xff0c;将…

小智 AI 聊天机器人

小智 AI 聊天机器人 &#xff08;XiaoZhi AI Chatbot&#xff09; &#x1f449;参考源项目复现 &#x1f449; ESP32SenseVoiceQwen72B打造你的AI聊天伴侣&#xff01;【bilibili】 &#x1f449; 手工打造你的 AI 女友&#xff0c;新手入门教程【bilibili】 项目目的 本…

MySql运维篇---008:日志:错误日志、二进制日志、查询日志、慢查询日志,主从复制:概述 虚拟机更改ip注意事项

#先登录mysql mysql -uroot -p1234#通过此系统变量&#xff0c;查看当前mysql的版本中默认的日志格式是哪个 show variables like %binlog\_format%;1.2.3 查看 由于日志是以二进制方式存储的&#xff0c;不能直接读取&#xff0c;需要通过二进制日志查询工具 mysqlbinlog 来查…

理解DeepSeek源代码之如何安装triton包

DeepSeek选择了开源路线&#xff0c;在github上可以下载到所有的源代码还有参数&#xff08;数据集应该没有开源&#xff09;&#xff0c;大语言模型的源代码规模其实非常小&#xff0c;DeepSeek V3的模型函数不过804行&#xff0c;阅读源代码有助于更好理解大语言模型。 1. D…

C++ Primer 标准库类型string

欢迎阅读我的 【CPrimer】专栏 专栏简介&#xff1a;本专栏主要面向C初学者&#xff0c;解释C的一些基本概念和基础语言特性&#xff0c;涉及C标准库的用法&#xff0c;面向对象特性&#xff0c;泛型特性高级用法。通过使用标准库中定义的抽象设施&#xff0c;使你更加适应高级…

为AI聊天工具添加一个知识系统 之83 详细设计之24 度量空间之1 因果关系和过程:认知金字塔

本文要点 度量空间 在本项目&#xff08;为AI聊天工具添加一个知识系统 &#xff09;中 是出于对“用”的考量 来考虑的。这包括&#xff1a; 相对-位置 力用&#xff08;“相”&#xff09;。正如 法力&#xff0c;相关-速度 体用 &#xff08;“体”&#xff09;。例如 重…

如何配置Java JDK

步骤1&#xff1a;点击资源&#xff0c;点击Java下载 https://www.oracle.com/ 步骤2&#xff1a;点击java下载、JDK23下载&#xff0c;下载第一行第一个 步骤3:解压到一个空文件夹下&#xff0c;复制lib地址 步骤4&#xff1a;在设置里面搜索“高级系统设置”&#xff1b;点击…

CodeGPT使用本地部署DeepSeek Coder

目前NV和github都托管了DeepSeek&#xff0c;生成Key后可以很方便的用CodeGPT接入。CodeGPT有三种方式使用AI&#xff0c;分别时Agents&#xff0c;Local LLMs&#xff08;本地部署AI大模型&#xff09;&#xff0c;LLMs Cloud Model&#xff08;云端大模型&#xff0c;从你自己…

JAVA安全—反射机制攻击链类对象成员变量方法构造方法

前言 还是JAVA安全&#xff0c;哎&#xff0c;真的讲不完&#xff0c;太多啦。 今天主要是讲一下JAVA中的反射机制&#xff0c;因为反序列化的利用基本都是要用到这个反射机制&#xff0c;还有一些攻击链条的构造&#xff0c;也会用到&#xff0c;所以就讲一下。 什么是反射…

【深度学习】softmax回归的简洁实现

softmax回归的简洁实现 我们发现(通过深度学习框架的高级API能够使实现)(softmax)线性(回归变得更加容易)。 同样&#xff0c;通过深度学习框架的高级API也能更方便地实现softmax回归模型。 本节继续使用Fashion-MNIST数据集&#xff0c;并保持批量大小为256。 import torch …

基础篇03-图像的基本运算

本节将简要介绍Halcon中有关图像的两类基本运算&#xff0c;分别是代数运算和逻辑运算。除此之外&#xff0c;还介绍几种特殊的代数运算。 目录 1.引言 2. 基本运算 2.1 加法运算 2.2 减法运算 2.3 乘法运算 2.4 除法运算 2.5 综合实例 3. 逻辑运算 3.1 逻辑与运算 …

工具的应用——安装copilot

一、介绍Copilot copilot是一个AI辅助编程的助手&#xff0c;作为需要拥抱AI的程序员可以从此尝试进入&#xff0c;至于好与不好&#xff0c;应当是小马过河&#xff0c;各有各的心得。这里不做评述。重点在安装copilot的过程中遇到了一些问题&#xff0c;然后把它总结下&…

Alibaba开发规范_编程规约之命名风格

文章目录 命名风格的基本原则1. 命名不能以下划线或美元符号开始或结束2. 严禁使用拼音与英文混合或直接使用中文3. 类名使用 UpperCamelCase 风格&#xff0c;但以下情形例外&#xff1a;DO / BO / DTO / VO / AO / PO / UID 等4. 方法名、参数名、成员变量、局部变量使用 low…

MATLAB中的IIR滤波器设计

在数字信号处理中&#xff0c;滤波器是消除噪声、提取特征或调整信号频率的核心工具。其中&#xff0c;无限脉冲响应&#xff08;IIR&#xff09;滤波器因其低阶数实现陡峭滚降的特性&#xff0c;被广泛应用于音频处理、通信系统和生物医学工程等领域。借助MATLAB强大的工具箱&…

vector容器(详解)

本文最后是模拟实现全部讲解&#xff0c;文章穿插有彩色字体&#xff0c;是我总结的技巧和关键 1.vector的介绍及使用 1.1 vector的介绍 https://cplusplus.com/reference/vector/vector/&#xff08;vector的介绍&#xff09; 了解 1. vector是表示可变大小数组的序列容器。…

python学opencv|读取图像(五十二)使用cv.matchTemplate()函数实现最佳图像匹配

【1】引言 前序学习了图像的常规读取和基本按位操作技巧&#xff0c;相关文章包括且不限于&#xff1a; python学opencv|读取图像-CSDN博客 python学opencv|读取图像&#xff08;四十九&#xff09;原理探究&#xff1a;使用cv2.bitwise()系列函数实现图像按位运算-CSDN博客…

【VUE案例练习】前端vue2+element-ui,后端nodo+express实现‘‘文件上传/删除‘‘功能

近期在做跟毕业设计相关的数据后台管理系统&#xff0c;其中的列表项展示有图片展示&#xff0c;添加/编辑功能有文件上传。 “文件上传/删除”也是我们平时开发会遇到的一个功能&#xff0c;这里分享个人的实现过程&#xff0c;与大家交流谈论~ 一、准备工作 本次案例使用的…