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 ...
随机推荐
- 实验一 Linux基础与Java开发环境
实验一 (一)实验内容 基于命令行和IDE(Intellj IDEA 简易教程http://www.cnblogs.com/rocedu/p/4421202.html)进行简单的Java程序编辑.编译 ...
- LeetCode 28:实现strStr() Implement strStr()
爱写bug(ID:icodebugs) 作者:爱写bug 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needl ...
- Laravel源码解析之model(代码)
本篇文章给大家带来的内容是关于Laravel源码解析之model(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 前言 提前预祝猿人们国庆快乐,吃好.喝好.玩好,我会在电视上看 ...
- [转帖]腾讯云TStack获下一代云计算技术创新奖 与鲲鹏等产品实现兼容性测试
http://www.techweb.com.cn/cloud/2019-12-16/2769286.shtml [TechWeb]12 月 16 日消息,在中国电子技术标准化研究院主办的“第九届中国 ...
- Java for循环每次都通过list.size()和 string.length()获取大小是否消耗性能?
前言 有人说在for循环之前用一个局部变量先获取到list.size().str.length(),然后在for循环的判断条件里通过这个局部变量替换list.size().str.length()会节 ...
- 不依赖Spring使用AspectJ达到AOP面向切面编程
网上大多数介绍AspectJ的文章都是和Spring容器混用的,但有时我们想自己写框架就需要抛开Spring造轮子,类似使用原生AspectJ达到面向切面编程.步骤很简单,只需要两步. 1.导入依赖 ...
- 用vscode开发vue应用
阅读 3237 收藏 205 2019-05-02 原文链接:segmentfault.com 云服务器 1 核 2G , 9元/月 ,买十送二,99/年!!!快来上车!developer.huawe ...
- [转] js网络请求跨域问题汇总(携带cookie)
前端程序使用extjs写,在本地测试,发送请求到服务器时,发现存在跨域的问题,cookie也没有set成功,于是乎在这里整理一下解决过程 由于篇幅较长,不想看解决过程的可以翻到最后看总结1.跨域允许2 ...
- layui提示框事件
使用layui提示框的时候遇到个问题,点击“确定”“取消”之类的按钮会执行里面的方法,但点击×就不会执行,所以在添加数据的时候出现个BUG,就是保存数据后点击弹出提示框的×,继续保存,如此循环,就可以 ...
- Mac系统docker初探
最近把工作环境要切到mac中,由于一直想看看docker是怎么回事,以前在win和linux下面都没有用起来,这次在mac中决定试一把,尝试下新的环境部署方式. 安装docker mac中,直接有类似 ...