LeetCode 622. Design Circular Queue
原题链接在这里: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();
*/
LeetCode 622. Design Circular Queue的更多相关文章
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...
- 【leetcode】622. Design Circular Queue
题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- LeetCode 622:设计循环队列 Design Circular Queue
LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...
- LeetCode 641. Design Circular Deque
原题链接在这里:https://leetcode.com/problems/design-circular-deque/ 题目: Design your implementation of the c ...
- [LeetCode] Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- C#LeetCode刷题之#622-设计循环队列(Design Circular Queue)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...
- [Swift]LeetCode622. 设计循环队列 | Design Circular Queue
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
随机推荐
- 第19课 lambda vs std::bind
一. std::bind (一)std::bind实现的关键技术 [编程实验]探索bind原理,实现自己的bind函数 #include <iostream> #include <t ...
- 被synchronized修饰的方法调用了没有被synchronized修饰的方法,是否是线程安全
1 被synchronized修饰的方法调用了没有被synchronized修饰的方法,是否线程安全? /** * (1)被synchronized修饰的方法调用了没有被synchronized修饰的 ...
- spring的15个经典面试题
总结Spring框架的15个经典面试题. 什么是Spring框架? Spring是一种轻量级框架,旨在提高开发人员的开发效率以及系统的可维护性. 我们一般说的Spring框架就是Spring Fram ...
- [4]Hexo静态博客背景及界面显示优化配置
示例预览:我的主页 前提预设: [3]hexo+github搭建个人博客的主题配置 [2]hexo+github搭建个人博客的简单使用 [1]hexo+github搭建个人博客的过程记录 背景图片添加 ...
- C#操作XML文档
Note: '=> ' 表示返回值 参考资料:请点击这里! 1:创建Xml文档 2:写Xml文档(必须保证有根元素) XmlDocument Xd (实例化一个对象) CreateXmlDecl ...
- 读取数据,并以txt格式保存
/// <summary> /// 读取数据,并以txt格式保存 /// </summary> /// <param name="data">数 ...
- Mybatis映射文件标签(关于sql)
Mybatis映射文件 1.接口的全限定名和映射文件的namespace一致 <mapper namespace="com.offcn.dao.UserDao"> 2. ...
- 编译OpenCV提示opencv_contrib缺少boostdesc_bgm.i等文件
错误提示: ~/opencv_contrib/modules/xfeatures2d/src/boostdesc.:: fatal error: boostdesc_bgm.i: No such fi ...
- vim 如何复制文件中多行到另一个文件
1.打开文件 vim a.txt b.tx 或者 vim *.txt 2.文件间切换 :n 切换到下一个文件 :wn 保存再切换 :N 到上一个文件 :wN 保存再切换 :.= 看当前行 3.假定当前 ...
- ES6 函数的拓展(四)
一.参数带默认值函数1.在函数形参可以赋予函数默认值[即实参严格匹配undefined时,在函数内部使用形参时调用它的默认值]2.函数name属性 [返回函数名称,无名的函数返回空字符串]3.函数le ...