原题链接在这里:https://leetcode.com/problems/design-circular-queue/

题目:

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.

Your implementation should support following operations:

  • MyCircularQueue(k): Constructor, set the size of the queue to be k.
  • Front: Get the front item from the queue. If the queue is empty, return -1.
  • Rear: Get the last item from the queue. If the queue is empty, return -1.
  • enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
  • deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
  • isEmpty(): Checks whether the circular queue is empty or not.
  • isFull(): Checks whether the circular queue is full or not.

Example:

  1. MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
  2. circularQueue.enQueue(1);  // return true
  3. circularQueue.enQueue(2);  // return true
  4. circularQueue.enQueue(3);  // return true
  5. circularQueue.enQueue(4);  // return false, the queue is full
  6. circularQueue.Rear();  // return 3
  7. circularQueue.isFull();  // return true
  8. circularQueue.deQueue();  // return true
  9. circularQueue.enQueue(4);  // return true
  10. circularQueue.Rear();  // return 4

Note:

  • All values will be in the range of [0, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in Queue library.

题解:

Use an array to mimic queue.

Have start to pointing to start of queue.

Have end to poining to end of queue.

Have len to keep track of size.

When enQueue, end = (end + 1) % k, assign arr[end].

When deQueue, start = (start + 1) % k.

Rear() return arr[end].

Front() return arr[start].

Time Complexity: MyCircularQueue, O(1). endQueue, O(1). deQueue(), O(1). Front, O(1). Rear, O(1). isEmpty, O(1). isFull, O(1).

Space: O(k).

AC Java:

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

类似Design Circular Deque.

LeetCode 622. Design Circular Queue的更多相关文章

  1. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  2. 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...

  3. 【leetcode】622. Design Circular Queue

    题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...

  4. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  5. LeetCode 622:设计循环队列 Design Circular Queue

    LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...

  6. LeetCode 641. Design Circular Deque

    原题链接在这里:https://leetcode.com/problems/design-circular-deque/ 题目: Design your implementation of the c ...

  7. [LeetCode] Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  8. C#LeetCode刷题之#622-设计循环队列​​​​​​​(Design Circular Queue)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...

  9. [Swift]LeetCode622. 设计循环队列 | Design Circular Queue

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

随机推荐

  1. Semaphore可以控制并发访问的线程个数

    public class SemaphoreTest { //信号量,只允许 3个线程同时访问 ); public static void main(String[] args) { Executor ...

  2. Java自学-类和对象 访问修饰符

    Java的四种访问修饰符 成员变量有四种修饰符 private 私有的 package/friendly/default 不写 protected 受保护的 public 公共的 比如public 表 ...

  3. Java学习:方法的引用

    方法引用(Method references) lambda表达式允许我们定义一个匿名方法,并允许我们以函数式接口的方式使用它.我们也希望能够在已有的方法上实现同样的特性. 方法引用和lambda表达 ...

  4. 【Python3爬虫】最新的12306爬虫

    一.写在前面 我在以前写过一次12306网站的爬虫,当时实现了模拟登录和查询车票,但是感觉还不太够,所以对之前的代码加以修改,还实现了一个订购车票的功能. 二.主要思路 在使用Selenium做模拟登 ...

  5. html页面的渲染And<script>位置的影响

    周末加班敲代码的时用到了<script>标签,突然想到了一个问题:别的自测项目里面<script>我把他放在了不同位置,这里应该会对代码的执行与渲染后影响吧?于是今天专门进行了 ...

  6. Dapper学习(一)之Execute和Query

    Dapper是一个用于.NET的简单的对象映射,并且在速度上有着轻ORM之王的称号. Dapper扩展IDbConnection,提供有用的扩展方法来查询数据库. 那么Dapper是怎样工作的呢? 总 ...

  7. delegate里的Invoke和BeginInvoke

    Invoke和BeginInvoke都是调用委托实体的方法,前者是同步调用,即它运行在主线程上,当Invode处理时间长时,会出现阻塞的情况,而BeginInvod是异步操作,它会从新开启一个线程,所 ...

  8. spring事务概念与获取事务时事务传播行为源码分析

    一.事务状态:org.springframework.transaction.TransactionStatus isNewTransaction 是否是新事务 hasSavepoint 是否有保存点 ...

  9. #pragma once和#ifndef

    C语言中的头文件只是简单的复制粘贴. C语言中变量.函数.结构体的定义和声明两个过程是分离的.声明通常放在头文件中,为了防止重复声明,需要保证头文件中的内容在构建obj文件时只被包含一次.这可以通过# ...

  10. 使用Git Bash向GitHub上传本地项目

    第一步:下载Git Bash(https://gitforwindows.org/),安装的过程是一路下一步,就不细说啦: 第二步:打开Git Bash,如下图显示: 第三步:现在让我们先放一放Git ...