• ​力扣解法汇总641-设计循环双端队列


     目录链接:

    力扣编程题-解法汇总_分享+记录-CSDN博客

    GitHub同步刷题项目:

    GitHub - September26/java-algorithms: 算法题汇总,包含牛客,leetCode,lintCode等网站题目的解法和代码,以及完整的mode类,甚至链表代码生成工具都有提供。

    原题链接:力扣


    描述:

    设计实现双端队列。

    实现 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 次

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/design-circular-deque
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    解题思路:

    * 解题思路:
    * 用集合和链表的方式都可以解决,但是链表的方式效率更高。
    * 构建一个具有头节点,尾节点,以及当前长度和最长长度的对象。
    * 插入式如果头节点为空,则头尾节点都设置为当前节点;
    * 否则插入到头节点之前。尾节点的方式类似
    * 删除头节点时,如果链表长度为1,则直接把头尾节点置为空,
    * 否则,删除头节点。

    代码:

    1. public class Solution641 {
    2. public static class MyCircularDeque {
    3. int currentLength = 0;
    4. int maxLength = 0;
    5. Node head;
    6. Node foot;
    7. public MyCircularDeque(int k) {
    8. this.maxLength = k;
    9. }
    10. public boolean insertFront(int value) {
    11. if (currentLength >= maxLength) {
    12. return false;
    13. }
    14. Node node = new Node();
    15. node.val = value;
    16. if (head == null) {
    17. head = node;
    18. foot = node;
    19. } else {
    20. node.next = head;
    21. head.previous = node;
    22. head = node;
    23. }
    24. currentLength++;
    25. return true;
    26. }
    27. public boolean insertLast(int value) {
    28. if (currentLength >= maxLength) {
    29. return false;
    30. }
    31. Node node = new Node();
    32. node.val = value;
    33. if (foot == null) {
    34. foot = node;
    35. head = node;
    36. } else {
    37. node.previous = foot;
    38. foot.next = node;
    39. foot = node;
    40. }
    41. currentLength++;
    42. return true;
    43. }
    44. public boolean deleteFront() {
    45. if (currentLength == 0) {
    46. return false;
    47. }
    48. if (currentLength == 1) {
    49. head = null;
    50. foot = null;
    51. } else {
    52. Node next = head.next;
    53. next.previous = null;
    54. head = next;
    55. }
    56. currentLength--;
    57. return true;
    58. }
    59. public boolean deleteLast() {
    60. if (currentLength == 0) {
    61. return false;
    62. }
    63. if (currentLength == 1) {
    64. head = null;
    65. foot = null;
    66. } else {
    67. Node previous = foot.previous;
    68. previous.next = null;
    69. foot = previous;
    70. }
    71. currentLength--;
    72. return true;
    73. }
    74. public int getFront() {
    75. if (head == null) {
    76. return -1;
    77. }
    78. return head.val;
    79. }
    80. public int getRear() {
    81. if (foot == null) {
    82. return -1;
    83. }
    84. return foot.val;
    85. }
    86. public boolean isEmpty() {
    87. return currentLength == 0;
    88. }
    89. public boolean isFull() {
    90. return currentLength == maxLength;
    91. }
    92. public static class Node {
    93. int val = 0;
    94. Node previous = null;
    95. Node next = null;
    96. }
    97. }
    98. }

  • 相关阅读:
    Ubuntu调整swap大小
    通过TCP Allocate连接数告警了解prometheus-NodeExporter数据采集及相关知识扩散
    29、分块式内存管理[malloc、free]
    【微服务】Nacos服务端完成微服务注册以及健康检查流程
    进程状态和优先级【Linux】
    成功解决ZooKeeper配置中出现Error contacting service. It is probably not running
    深入理解 JVM 垃圾回收机制及其实现原理
    C++超市商品管理系统
    【ZooKeeper 】安装和使用,以及java客户端
    Java中split方法简介
  • 原文地址:https://blog.csdn.net/AA5279AA/article/details/126341702