forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
36 lines (36 loc) · 822 Bytes
/
s1.cpp
File metadata and controls
36 lines (36 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// OJ: https://leetcode.com/problems/design-circular-queue/
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(K)
class MyCircularQueue {
private:
vector<int> v;
int start = 0, len = 0;
public:
MyCircularQueue(int k): v(k) {}
bool enQueue(int value) {
if (isFull()) return false;
v[(start + len++) % v.size()] = value;
return true;
}
bool deQueue() {
if (isEmpty()) return false;
start = (start + 1) % v.size();
--len;
return true;
}
int Front() {
if (isEmpty()) return -1;
return v[start];
}
int Rear() {
if (isEmpty()) return -1;
return v[(start + len - 1) % v.size()];
}
bool isEmpty() {
return !len;
}
bool isFull() {
return len == v.size();
}
};