原题链接在这里: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:

MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
circularQueue.enQueue(1);  // return true
circularQueue.enQueue(2);  // return true
circularQueue.enQueue(3);  // return true
circularQueue.enQueue(4);  // return false, the queue is full
circularQueue.Rear();  // return 3
circularQueue.isFull();  // return true
circularQueue.deQueue();  // return true
circularQueue.enQueue(4);  // return true
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:

 class MyCircularQueue {
int [] arr;
int start;
int end;
int len;
int k; /** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
arr = new int[k];
start = 0;
end = -1;
len = 0;
this.k = k;
} /** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if(isFull()){
return false;
} end = (end + 1) % k;
len++;
arr[end] = value;
return true;
} /** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if(isEmpty()){
return false;
} start = (start + 1) % k;
len--;
return true;
} /** Get the front item from the queue. */
public int Front() {
return isEmpty() ? -1 : arr[start];
} /** Get the last item from the queue. */
public int Rear() {
return isEmpty() ? -1 : arr[end];
} /** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return len == 0;
} /** Checks whether the circular queue is full or not. */
public boolean isFull() {
return len == k;
}
} /**
* 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();
*/

类似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. 2019年上-C语言程序设计-第1次blog作业

    准备工作(20分) 1.在博客园申请个人博客,以真实姓名和学号加入班级博客(链接地址) 2.关注邹欣老师博客(并寻找答案:邹欣老师是谁?是做什么的?).关注任课老师博客 3.注册登录中国大学MOOC网 ...

  2. go-gin-api 路由中间件 - Jaeger 链路追踪

    概述 首先同步下项目概况: 上篇文章分享了,路由中间件 - Jaeger 链路追踪(理论篇). 这篇文章咱们分享:路由中间件 - Jaeger 链路追踪(实战篇). 说实话,这篇文章确实让大家久等了, ...

  3. FormData使用详解

  4. Java 中要将 String 类型转化为 int 类型

    在 Java 中要将 String 类型转化为 int 类型时,需要使用 Integer 类中的 parseInt() 方法或者 valueOf() 方法进行转换. 例1: 1 2 3 4 5 6 S ...

  5. 脱离 WebView 的通信 JavaScriptCore

    JavascriptCore JavascriptCore 一直作为 WebKit 中内置的 JS 引擎使用,在 iOS7 之后,Apple 对原有的 C/C++ 代码进行了 OC 封装,成为系统级的 ...

  6. C# Newtonsoft.Json.JsonReaderException:“Could not convert string to decimal:

    使用Newtonsoft.Json,报以上错误,问题的原因是有"",把“”替换成null: 以前的json: [{"WengvNj":"df5c38c ...

  7. C# 身份证号码15位和18位验证

    /// <summary> /// 身份证 /// </summary> [Serializable] public class IDCard {     /// <su ...

  8. delegate里的Invoke和BeginInvoke

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

  9. because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checkin

    1 前言 浏览器报错误(chrome和firefox都会):because its MIME type ('text/html') is not a supported stylesheet MIME ...

  10. python 排序 桶排序

    算法思想: 桶排序将数组分到有限数量的桶里.然后每个桶里再分别排序(使用任何算法) 当要倍排序的数组内的数值时均匀分配的时候,桶排序使用线性时间O(n) 步骤: 根据最大值.最小值.桶内数据范围设定一 ...