当前位置: 首页 > news >正文

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

设计循环双端队列

  • 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.lryc.cn/news/530500.html

相关文章:

  • 从零开始学 HTML:构建网页的基本框架与技巧
  • 一些杂记2
  • C语言 --- 分支
  • 【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】2.10 ndarray内存模型:从指针到缓存优化
  • 【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】2.6 广播机制核心算法:维度扩展的数学建模
  • K8S极简教程(4小时快速学会)
  • 系统URL整合系列视频二(界面原型)
  • 虚幻浏览器插件 UE与JS通信
  • OpenAI深夜反击:o3-mini免费上线,能否撼动DeepSeek的地位?
  • Golang 应用的 Docker 部署方式介绍及使用详解
  • deep seek R1本地化部署及openAI API调用
  • 力扣第435场周赛讲解
  • 初入机器学习
  • Signature
  • 93,【1】buuctf web [网鼎杯 2020 朱雀组]phpweb
  • 笔灵ai写作技术浅析(四):知识图谱
  • Chromium132 编译指南 - Android 篇(四):配置 depot_tools
  • 使用真实 Elasticsearch 进行高级集成测试
  • SQL进阶实战技巧:如何分析浏览到下单各步骤转化率及流失用户数?
  • 机器学习--概览
  • 低代码系统-产品架构案例介绍、炎黄盈动-易鲸云(十二)
  • Electricity Market Optimization 探索系列(二)
  • OpenAI 实战进阶教程 - 第一节:OpenAI API 架构与基础调用
  • TensorFlow简单的线性回归任务
  • 【视频+图文详解】HTML基础4-html标签的基本使用
  • 在Arm芯片苹果Mac系统上通过homebrew安装多版本mysql并解决各种报错,感谢deepseek帮助解决部分问题
  • c++可变参数详解
  • 【深度分析】DeepSeek 遭暴力破解,攻击 IP 均来自美国,造成影响有多大?有哪些好的防御措施?
  • CMake项目编译与开源项目目录结构
  • 完全卸载mysql server步骤