-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC641.java
More file actions
99 lines (87 loc) · 2.11 KB
/
LC641.java
File metadata and controls
99 lines (87 loc) · 2.11 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* LC641
*/
import java.util.Deque;
import java.util.LinkedList;
class MyCircularDeque {
// Declaration
Deque<Integer> d;
int size;
public MyCircularDeque(int k) {
// Initialization in Constructor
d = new LinkedList<>();
size = k;
}
public boolean insertFront(int value) {
// Insert element from First
if (d.size() < size) {
d.addFirst(value);
return true;
}
return false;
}
public boolean insertLast(int value) {
// Insert element from Last
if (d.size() < size) {
d.addLast(value);
return true;
}
return false;
}
public boolean deleteFront() {
// Remove element from First
if (d.size() > 0) {
d.removeFirst();
return true;
}
return false;
}
public boolean deleteLast() {
// Remove element from Last
if (d.size() > 0) {
d.removeLast();
return true;
}
return false;
}
public int getFront() {
// Get the first element
if (d.size() <= 0) {
return -1;
}
return d.getFirst();
}
public int getRear() {
// Get the last element
if (d.size() <= 0) {
return -1;
}
return d.getLast();
}
public boolean isEmpty() {
// Check the deque is empty or not
if (d.size() == 0) {
return true;
}
return false;
}
public boolean isFull() {
// Check the deque is full or not
if (d.size() == size) {
return true;
}
return false;
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque obj = new MyCircularDeque(k);
* boolean param_1 = obj.insertFront(value);
* boolean param_2 = obj.insertLast(value);
* boolean param_3 = obj.deleteFront();
* boolean param_4 = obj.deleteLast();
* int param_5 = obj.getFront();
* int param_6 = obj.getRear();
* boolean param_7 = obj.isEmpty();
* boolean param_8 = obj.isFull();
*/