← 返回 bytedance 的题目列表Design Circular Queue
类型:online_judge
Problem: Design Circular Queue
Implement a circular queue MyCircularQueue with a fixed capacity k.
A circular queue is a linear data structure where the tail position wraps around to the head position. It supports FIFO operations and can reuse empty positions at the beginning of the underlying array after reaching the end.
You need to implement the following methods:
MyCircularQueue(k): Initializes the queue with capacity k.
enQueue(value): Inserts an element into the circular queue. Return true if the operation is successful; otherwise return false if the queue is full.
deQueue(): Deletes an element from the circular queue. Return true if the operation is successful; otherwise return false if the queue is empty.
Front(): Returns the front item. Return -1 if the queue is empty.
Rear(): Returns the rear item. Return -1 if the queue is empty.
isEmpty(): Checks whether the circular queue is empty.
isFull(): Checks whether the circular queue is full.
Input Format
For testing, use the LeetCode-style format:
The first line is an array of operation names, e.g. ["MyCircularQueue","enQueue","Rear"]
The second line is an array of argument lists, e.g. [[3],[1],[]]
Output Format
Output an array of return values for all operations:
The constructor returns null
Boolean values should be printed as JSON true / false
Example
Input:
["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"]
[[3],[1],[2],[3],[4],[],[],[],[4],[]]
Output:
[null,true,true,true,false,3,true,true,true,4]
Constraints
1 <= k <= 1000
0 <= value <= 1000
At most 3000 method calls will be made.
Example
Input
["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"]
[[3],[1],[2],[3],[4],[],[],[],[4],[]]
Output
[null,true,true,true,false,3,true,true,true,4]