【LeetCode】设计数据结构 | List、Stack、Queue、DLinkedList

【LeetCode】设计数据结构|List、Stack、Queue、DLinkedList

文章目录

    • 【LeetCode】设计数据结构|List、Stack、Queue、DLinkedList
    • @[toc]
      • 设计链表(中等)
      • 用栈实现队列(简单)
      • 用队列实现栈(简单)
      • 设计循环队列(中等)
      • 设计循环双端队列(中等)
      • 设计前中后队列(中等)

设计链表(中等)

707. 设计链表

冗余版

class MyLinkedList {class ListNode {int val;ListNode next;ListNode(int val) {this.val = val;}}int size;ListNode dummyHead;public MyLinkedList() {this.size = 0;this.dummyHead = new ListNode(-1);}public int get(int index) {if (index + 1 > size) return -1;ListNode node = dummyHead.next;for (int i = 0; i < index; i++) {node = node.next;}return node.val;}public void addAtHead(int val) {ListNode newNode = new ListNode(val);newNode.next = dummyHead.next;dummyHead.next = newNode;size++;}public void addAtTail(int val) {ListNode node = dummyHead;while (node.next != null) {node = node.next;}node.next = new ListNode(val);size++;}public void addAtIndex(int index, int val) {ListNode newNode = new ListNode(val);ListNode node = dummyHead;for (int i = 0; i < index; i++) {node = node.next;if (node == null) return;}newNode.next = node.next;node.next = newNode;size++;}public void deleteAtIndex(int index) {if (index < 0 || index >= size) return;ListNode node = dummyHead;for (int i = 0; i < index; i++) {node = node.next;}ListNode deleteNode = node.next;node.next = deleteNode.next;// 清除野指针deleteNode = null;size--;}
}

代码复用简化版

class MyLinkedList {class ListNode {int val;ListNode next;ListNode(int val) {this.val = val;}}int size;ListNode dummyHead;public MyLinkedList() {this.size = 0;this.dummyHead = new ListNode(-1);}public int get(int index) {if (index + 1 > size) return -1;ListNode node = dummyHead.next;for (int i = 0; i < index; i++) {node = node.next;}return node.val;}public void addAtHead(int val) {this.addAtIndex(0, val);}public void addAtTail(int val) {this.addAtIndex(size, val);}public void addAtIndex(int index, int val) {ListNode newNode = new ListNode(val);ListNode node = dummyHead;for (int i = 0; i < index; i++) {node = node.next;if (node == null) return;}newNode.next = node.next;node.next = newNode;size++;}public void deleteAtIndex(int index) {if (index < 0 || index >= size) return;ListNode node = dummyHead;for (int i = 0; i < index; i++) {node = node.next;}ListNode deleteNode = node.next;node.next = deleteNode.next;// 清除野指针deleteNode = null;size--;}
}

用栈实现队列(简单)

232. 用栈实现队列

class MyQueue {Stack<Integer> a;Stack<Integer> b;public MyQueue() {this.a = new Stack<Integer>();this.b = new Stack<Integer>();}public void push(int x) {a.push(x);}public int pop() {if (b.isEmpty()) {while (!a.isEmpty()) {b.push(a.pop());}}return b.pop();}public int peek() {if (b.isEmpty()) {while (!a.isEmpty()) {b.push(a.pop());}}return b.peek();}public boolean empty() {return a.isEmpty() && b.isEmpty();}
}

用队列实现栈(简单)

225. 用队列实现栈

方法一:双队列实现

class MyStack {Queue<Integer> a;Queue<Integer> b;public MyStack() {this.a = new LinkedList<Integer>();this.b = new LinkedList<Integer>();}public void push(int x) {while (!a.isEmpty()) {b.offer(a.poll());}a.offer(x);while (!b.isEmpty()) {a.offer(b.poll());}}public int pop() {return a.poll();}public int top() {return a.peek();}public boolean empty() {return a.isEmpty();}
}

方法二:单队列实现

class MyStack {Queue<Integer> a;public MyStack() {this.a = new LinkedList<Integer>();}public void push(int x) {a.offer(x);for (int i = 0; i < a.size() - 1; i++) {a.offer(a.poll());}}public int pop() {return a.poll();}public int top() {return a.peek();}public boolean empty() {return a.isEmpty();}
}

设计循环队列(中等)

622. 设计循环队列

使用数组实现

class MyCircularQueue {private int capacity;private int front, rear;private int[] elements;public MyCircularQueue(int k) {this.capacity = k + 1;this.front = this.rear = 0;this.elements = new int[k + 1];}public boolean enQueue(int value) {if (isFull()) return false;elements[rear] = value;rear = (rear + 1) % capacity;return true;}public boolean deQueue() {if (isEmpty()) return false;front = (front + 1) % capacity;return true;}public int Front() {return isEmpty() ? -1 : elements[front];}public int Rear() {return isEmpty() ? -1 : elements[(rear - 1 + capacity) % capacity];}public boolean isEmpty() {return rear == front;}public boolean isFull() {return (rear + 1) % capacity == front;}
}

使用链表实现

class MyCircularQueue {class ListNode {int val;ListNode next;ListNode(int val) {this.val = val;}}int capacity, size;ListNode head, tail;public MyCircularQueue(int k) {this.size = 0;this.capacity = k;}public boolean enQueue(int value) {if (isFull()) return false;ListNode node = new ListNode(value);if (isEmpty()) {head = tail = node;} else {tail.next = node;tail = node;}size++;return true;}public boolean deQueue() {if (isEmpty()) return false;ListNode node = head;head = head.next;node = null;size--;return true;}public int Front() {return isEmpty() ? -1 : head.val;}public int Rear() {return isEmpty() ? -1 : tail.val;}public boolean isEmpty() {return size == 0;}public boolean isFull() {return size == capacity;}
}

设计循环双端队列(中等)

641. 设计循环双端队列

方法一:数组实现

class MyCircularDeque {private int capacity;private int front, rear;private int[] elements;public MyCircularDeque(int k) {this.capacity = k + 1;this.front = this.rear = 0;this.elements = new int[k + 1];}public boolean insertFront(int value) {if (isFull()) return false;front = (front - 1 + capacity) % capacity;elements[front] = value;return true;}public boolean insertLast(int value) {if (isFull()) return false;elements[rear] = value;rear = (rear + 1 + capacity) % capacity;return true;}public boolean deleteFront() {if (isEmpty()) return false;front = (front + 1) % capacity;return true;}public boolean deleteLast() {if (isEmpty()) return false;rear = (rear - 1 + capacity) % capacity;return true;}public int getFront() {return isEmpty() ? -1 : elements[front];}public int getRear() {return isEmpty() ? -1 : elements[(rear - 1 + capacity) % capacity];}public boolean isEmpty() {return front == rear;}public boolean isFull() {return (rear + 1) % capacity == front;}
}

方法二:链表实现

class MyCircularDeque {class DLinkedNode {int val;DLinkedNode prev;DLinkedNode next;DLinkedNode (int val) {this.val = val;}}private int size, capacity;private DLinkedNode dummyHead, dummyTail;public MyCircularDeque(int k) {this.size = 0;this.capacity = k;// 设置一个虚的头结点和尾节点this.dummyHead = new DLinkedNode(-1);this.dummyTail = new DLinkedNode(-1);this.dummyHead.next = this.dummyTail;this.dummyTail.prev = this.dummyHead;}public boolean insertFront(int value) {if (isFull()) return false;DLinkedNode node = new DLinkedNode(value);node.next = dummyHead.next;node.prev = dummyHead;dummyHead.next.prev = node;dummyHead.next = node;size++;return true;}public boolean insertLast(int value) {if (isFull()) return false;DLinkedNode node = new DLinkedNode(value);node.next = dummyTail;node.prev = dummyTail.prev;dummyTail.prev.next = node;dummyTail.prev = node;size++;return true;}public boolean deleteFront() {if (isEmpty()) return false;DLinkedNode node = dummyHead.next;node.next.prev = dummyHead;dummyHead.next = node.next;node = null;size--;return true;}public boolean deleteLast() {if (isEmpty()) return false;DLinkedNode node = dummyTail.prev;node.next.prev = node.prev;node.prev.next = node.next;node = null;size--;return true;}public int getFront() {return isEmpty() ? -1 : dummyHead.next.val;}public int getRear() {return isEmpty() ? -1 : dummyTail.prev.val;}public boolean isEmpty() {// return dummyHead.next == dummyTail;// return dummyTail.prev == dummyHead;return size == 0;}public boolean isFull() {return size == capacity;}
}

设计前中后队列(中等)

1670. 设计前中后队列

class FrontMiddleBackQueue {// 保持  leftQueue.size() == rightQueue.size()//  or  leftQueue.size() + 1 == rightQueue.size()// 注意中位数是按进入数字流中的数字的先后顺序来看的(别搞反啦!!!)private LinkedList<Integer> leftQueue;private LinkedList<Integer> rightQueue;public FrontMiddleBackQueue() {leftQueue  = new LinkedList<Integer>();rightQueue = new LinkedList<Integer>();}public void pushFront(int val) {if (leftQueue.size() == rightQueue.size()) {rightQueue.offerFirst(val);} else {leftQueue.offerFirst(rightQueue.pollLast());rightQueue.offerFirst(val);}}public void pushMiddle(int val) {if (rightQueue.isEmpty()) {rightQueue.offerLast(val);} else if (leftQueue.size() == rightQueue.size()) {rightQueue.offerLast(val);} else {leftQueue.offerFirst(rightQueue.pollLast());rightQueue.offerLast(val);}}public void pushBack(int val) {leftQueue.offerLast(val);if (leftQueue.size() > rightQueue.size()) {rightQueue.offerLast(leftQueue.pollFirst());}}public int popFront() {if (rightQueue.isEmpty()) return -1;int val = rightQueue.pollFirst();if (leftQueue.size() > rightQueue.size()) {rightQueue.offerLast(leftQueue.pollFirst());}return val;}public int popMiddle() {if (rightQueue.isEmpty()) return -1;int val = rightQueue.pollLast();if (leftQueue.size() > rightQueue.size()) {rightQueue.offerLast(leftQueue.pollFirst());}return val;}public int popBack() {if (rightQueue.isEmpty()) return -1;if (leftQueue.size() < rightQueue.size()) {leftQueue.offerFirst(rightQueue.pollLast());}return leftQueue.pollLast();}
}

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

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

相关文章

十分钟配置好Neovim go开发环境(其他语言一样)

文章目录 前言仓库地址用法快捷键问题反馈 前言 这篇文章的目的是为了分享下我自己的Neovim配置。 本人是Golang程序员&#xff0c;最开始使用的IDE是JetBrains Goland。有一说一这个ide适配度很高&#xff0c;认识的很多人都使用这个。但是它也有几个对我来说的缺点&#xf…

青少年软件编程(Python) 等级考试试卷(五级)2023年5月

青少年软件编程&#xff08;Python&#xff09; 等级考试试卷&#xff08;五级&#xff09; 分数&#xff1a; 100 题数&#xff1a; 38 一、 单选题(共 25 题&#xff0c; 共 50 分) 1.有列表 L[ UK , china , lili , "张三"]&#xff0c; print(L[-2]) 的结果是&…

认识C语言

目录 1.C语言的发展历史 2.C语言的特点 3.C语言的应用领域 1.C语言的发展历史 C语言的发展历史可以追溯到20世纪60年代。在那个时候&#xff0c;计算机科学领域存在着多种编程语言&#xff0c;但它们在功能、效率和可移植性等方面存在一些限制。 C语言的起源可以追溯到贝尔实…

DataX将MySQL数据同步到HDFS中时,空值不处理可以吗

DataX将MySQL数据同步到HDFS中时&#xff0c;空值存到HDFS中时&#xff0c;默认是存储为\N&#xff0c;这样会有两个缺点&#xff1a; 会产生歧义&#xff0c;如果MySQL业务数据中有\N数据&#xff0c;那么存储到HDFS上是\N&#xff0c;null值存储也是\N&#xff0c;当用Hive查…

深入解析MySQL:类型、优势与使用实例

摘要&#xff1a;本文将深入探讨MySQL数据库的类型、为何选择这种类型的数据库以及其优点。通过详细的解释、具体的示例和输出结果&#xff0c;我们将全面介绍MySQL数据库的相关知识&#xff0c;并提供最后的百十字总结。 1. 引言 MySQL是一种常用的关系型数据库管理系统&…

在矩池云使用Llama2-7B的具体方法

今天给大家分享如何在矩池云服务器使用 Llama2-7b模型。 硬件要求 矩池云已经配置好了 Llama 2 Web UI 环境&#xff0c;显存需要大于 8G&#xff0c;可以选择 A4000、P100、3090 以及更高配置的等显卡。 租用机器 在矩池云主机市场&#xff1a;https://matpool.com/host-m…

linux下软件安装 - 与GPT对话

1 cmake 没有uninstall 你是对的,CMake 默认情况下并没有提供 make uninstall 目标。这是因为 make install 操作是在特定的目录下进行安装的,CMake 不知道安装到了哪里以及哪些文件被安装了。因此,没有直接支持的 make uninstall 目标来进行卸载。 如果你想要卸载通过 mak…

unity事件处理

方法调用 //发送事件 【发送事件码&#xff0c;发送消息内容】 EventCenterUtil.Broadcast(EventCenterUtil.EventType.Joystick, ui);//监听无参事件 EventCenterUtil.AddListener(EventCenterUtil.EventType.Joystick, show); public void show(){}//发送事件 有参事件 Eve…

自然语言处理从入门到应用——LangChain:提示(Prompts)-[基础知识]

分类目录&#xff1a;《自然语言处理从入门到应用》总目录 模型编程的新方法是使用提示&#xff08;Prompts&#xff09;。提示指的是模型的输入。这个输入通常由多个组件构成。PromptTemplate负责构建这个输入&#xff0c;LangChain提供了多个类和函数&#xff0c;使得构建和处…

快应用编译前如何统一替换字符串

假设你有一个需求&#xff0c;要把代码里的ad-button替换为div&#xff0c;因为是mi看ad-button不爽。 这还不简单么&#xff0c;webpack有那么多成熟的plugins和loaders&#xff0c;本身我对webpack也只是略知一二&#xff0c;随便一搜网上的解决方案&#xff0c; string-re…

发点实用的快捷键(mac

切换输入法&#xff1a;ctrlspace /ctrloptionspace&#xff08;更快捷 切换网页&#xff1a; shifttab 切换应用界面&#xff1a;alttab 关闭页面&#xff1a;altw 搜索&#xff1a;altspace 展示mac隐藏文件&#xff1a; Commangshift . (点) 以下是一些浏览器快捷键&am…

Windows磁盘清理

针对开发同学&#xff0c;磁盘不够用时&#xff0c;常见的需要清理的内容&#xff1a; 1、虚拟机镜像、Docker镜像等。 通常占用比较大的存储&#xff0c;一个实例从几个G到几十个G。 2、Maven本地仓库。 如果公司有私服&#xff0c;可以全部删掉重新依赖&#xff0c;否则不…

ArcGIS应用

ArcGIS产品线为用户提供一个可伸缩的&#xff0c;全面的GIS平台。ArcObjects包含了许多的可编程组件&#xff0c;从细粒度的对象&#xff08;例如单个的几何对象&#xff09;到粗粒度的对象&#xff08;例如与现有ArcMap文档交互的地图对象&#xff09;涉及面极广&#xff0c;这…

Spring优雅的在事务提交/回滚前后插入业务逻辑

业务背景 业务那边想要统计下我们这边每天注册商户成功和失败的数量&#xff0c;你看看怎么给他弄下这个功能 功能实现 TransactionSynchronizationManager.registerSynchronization&#xff0c;发现这是spring事务提供的注册回调接口的方法。 在事务注解方法中&#xff0c…

Flutter详解和代码实例

目录 1. Flutter 基础概念2. Flutter 核心架构3. Flutter 组件库4. Flutter 布局与渲染5. Flutter 网络请求6. Flutter 调试工具7.实例8.优缺点8.1 Flutter 框架的优点包括&#xff1a;8.2 Flutter 框架的缺点包括&#xff1a; Flutter 是一款由 Google 开发的跨平台移动应用开…

【嵌入式学习笔记】嵌入式入门1——GPIO

1.什么是GPIO General Purpose Input Output&#xff0c;即通用输入输出端口&#xff0c;简称GPIO&#xff0c;作用是负责采集外部器件的信息或者控制外部器件工作&#xff0c;即输入输出。 2.STM32 GPIO简介 2.1.GPIO特点 不同型号&#xff0c;IO口数量可能不一样&#x…

融合大数据、物联网和人工智能的智慧校园云平台源码 智慧学校源码

电子班牌系统用以展示各个班级的考勤信息、授课信息、精品课程、德育宣传、班级荣誉、校园电视台、考场信息、校园通知、班级风采&#xff0c;是智慧校园和智慧教室的对外呈现窗口&#xff0c;也是学校校园文化宣传和各种信息展示的重要载体。将大数据、物联网和人工智能等新兴…

1.2 eureka注册中心,完成服务注册

目录 环境搭建 搭建eureka服务 导入eureka服务端依赖 编写启动类&#xff0c;添加EnableEurekaServer注解 编写eureka配置文件 启动服务,访问eureka Euraka服务注册 创建了两个子模块 在模块里导入rureka客户端依赖 编写eureka配置文件 添加Services 环境搭建 创建父…

Moonbeam新增强大的互操作性功能至波卡生态

波卡上的领先多链开发平台Moonbeam发布适用于平行链间活动的新版本互操作性功能。最新的链更新Runtime 2401&#xff0c;是自Moonbeam上线后的最大更新&#xff0c;其中包括三个针对开发者于链上的里程碑式更新&#xff1a; 用于访问消息传递功能的预编译智能合约从其他波卡平…

Python批量将Excel内指定列的数据向上移动一行

本文介绍基于Python语言&#xff0c;针对一个文件夹下大量的Excel表格文件&#xff0c;对其中的每一个文件加以操作——将其中指定的若干列的数据部分都向上移动一行&#xff0c;并将所有操作完毕的Excel表格文件中的数据加以合并&#xff0c;生成一个新的Excel文件的方法。 首…