-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLC0622.cpp
executable file
·67 lines (58 loc) · 1.21 KB
/
LC0622.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Problem Statement: https://leetcode.com/problems/design-circular-queue/
Space: O(k)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
|--------------------|------|-------|
| Operations | Time | Space |
|--------------------|------|-------|
| MyCircularQueue(k) | O(k) | O(1) |
| enQueue(value) | O(1) | O(1) |
| deQueue() | O(1) | O(1) |
| Front() | O(1) | O(1) |
| Rear() | O(1) | O(1) |
| isEmpty() | O(1) | O(1) |
| isFull() | O(1) | O(1) |
|--------------------|------|-------|
*/
class MyCircularQueue {
private:
int beg, size;
vector<int> queue;
public:
MyCircularQueue(int k): beg(0), size(0), queue(k) {}
bool enQueue(int value) {
if (isFull())
return false;
size++;
queue[end()] = value;
return true;
}
bool deQueue() {
if (isEmpty())
return false;
size--;
beg = (beg + 1) % queue.size();
return true;
}
int end() {
return (beg + size - 1) % queue.size();
}
int Front() {
if (isEmpty())
return -1;
else
return queue[beg];
}
int Rear() {
if (isEmpty())
return -1;
else
return queue[end()];
}
bool isEmpty() {
return size == 0;
}
bool isFull() {
return size == queue.size();
}
};