Java实现 LeetCode 622 设计循环队列(暴力大法)

偏执的太偏执、 2023-07-19 10:47 22阅读 0赞

622. 设计循环队列

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

你的实现应该支持如下操作:

  1. MyCircularQueue(k): 构造器,设置队列长度为 k
  2. Front: 从队首获取元素。如果队列为空,返回 -1
  3. Rear: 获取队尾元素。如果队列为空,返回 -1
  4. enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
  5. deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
  6. isEmpty(): 检查循环队列是否为空。
  7. isFull(): 检查循环队列是否已满。

示例:

  1. MyCircularQueue circularQueue = new MycircularQueue(3); // 设置长度为 3
  2. circularQueue.enQueue(1); // 返回 true
  3. circularQueue.enQueue(2); // 返回 true
  4. circularQueue.enQueue(3); // 返回 true
  5. circularQueue.enQueue(4); // 返回 false,队列已满
  6. circularQueue.Rear(); // 返回 3
  7. circularQueue.isFull(); // 返回 true
  8. circularQueue.deQueue(); // 返回 true
  9. circularQueue.enQueue(4); // 返回 true
  10. circularQueue.Rear(); // 返回 4

提示:

所有的值都在 0 至 1000 的范围内;
操作数将在 1 至 1000 的范围内;
请不要使用内置的队列库。

  1. class MyCircularQueue {
  2. private Integer []arr;
  3. private int head;
  4. private int tail;
  5. /** Initialize your data structure here. Set the size of the queue to be k. */
  6. public MyCircularQueue(int k) {
  7. arr=new Integer[k];
  8. head=0;
  9. tail=0;
  10. }
  11. /** Insert an element into the circular queue. Return true if the operation is successful. */
  12. public boolean enQueue(int value) {
  13. if(isFull()) {
  14. return false;
  15. }else {
  16. arr[tail]=value;
  17. tail=(tail+1)%(arr.length);
  18. return true;
  19. }
  20. }
  21. /** Delete an element from the circular queue. Return true if the operation is successful. */
  22. public boolean deQueue() {
  23. if(isEmpty()) {
  24. return false;
  25. }else {
  26. arr[head]=null;
  27. head=(head+1)%(arr.length);
  28. return true;
  29. }
  30. }
  31. /** Get the front item from the queue. */
  32. public int Front() {
  33. if(isEmpty()) {
  34. return -1;
  35. }else {
  36. return arr[head];
  37. }
  38. }
  39. /** Get the last item from the queue. */
  40. public int Rear() {
  41. if(isEmpty()) {
  42. return -1;
  43. }else {
  44. if(tail!=0)return arr[tail-1];
  45. else return arr[arr.length-1];
  46. }
  47. }
  48. /** Checks whether the circular queue is empty or not. */
  49. public boolean isEmpty() {
  50. if(head==tail&&arr[head]==null) {
  51. return true;
  52. }else {
  53. return false;
  54. }
  55. }
  56. /** Checks whether the circular queue is full or not. */
  57. public boolean isFull() {
  58. if(head==tail&&arr[head]!=null) {
  59. return true;
  60. }else {
  61. return false;
  62. }
  63. }
  64. }
  65. /** * Your MyCircularQueue object will be instantiated and called as such: * MyCircularQueue obj = new MyCircularQueue(k); * boolean param_1 = obj.enQueue(value); * boolean param_2 = obj.deQueue(); * int param_3 = obj.Front(); * int param_4 = obj.Rear(); * boolean param_5 = obj.isEmpty(); * boolean param_6 = obj.isFull(); */

发表评论

表情:
评论列表 (有 0 条评论,22人围观)

还没有评论,来说两句吧...

相关阅读

    相关 leetcode622设计循环队列

    使用数组完成对循环队列的设计 空闲单元法:进行队列是否为空,是否为满的情况; front : 指向队列中第一个元素 rear : 指向队列中最后一个元素的