1
1
/**
2
- * [641] design circular deque
3
- * https://leetcode.cn/problems/design-circular-deque/
4
- *
5
- * 设计实现双端队列。
6
- *
7
- * 实现 MyCircularDeque 类:
8
- *
9
- * MyCircularDeque(int k) :构造函数,双端队列最大为 k 。
10
- * boolean insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true ,否则返回 false 。
11
- * boolean insertLast() :将一个元素添加到双端队列尾部。如果操作成功返回 true ,否则返回 false 。
12
- * boolean deleteFront() :从双端队列头部删除一个元素。 如果操作成功返回 true ,否则返回 false 。
13
- * boolean deleteLast() :从双端队列尾部删除一个元素。如果操作成功返回 true ,否则返回 false 。
14
- * int getFront() ):从双端队列头部获得一个元素。如果双端队列为空,返回 -1 。
15
- * int getRear() :获得双端队列的最后一个元素。 如果双端队列为空,返回 -1 。
16
- * boolean isEmpty() :若双端队列为空,则返回 true ,否则返回 false 。
17
- * boolean isFull() :若双端队列满了,则返回 true ,否则返回 false 。
18
- *
19
- *
20
- * 示例 1:
21
- *
22
- * 输入
23
- * ["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
24
- * [[3], [1], [2], [3], [4], [], [], [], [4], []]
25
- * 输出
26
- * [null, true, true, true, false, 2, true, true, true, 4]
27
- *
28
- * 解释
29
- * MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
30
- * circularDeque.insertLast(1); // 返回 true
31
- * circularDeque.insertLast(2); // 返回 true
32
- * circularDeque.insertFront(3); // 返回 true
33
- * circularDeque.insertFront(4); // 已经满了,返回 false
34
- * circularDeque.getRear(); // 返回 2
35
- * circularDeque.isFull(); // 返回 true
36
- * circularDeque.deleteLast(); // 返回 true
37
- * circularDeque.insertFront(4); // 返回 true
38
- * circularDeque.getFront(); // 返回 4
39
-
40
- * 提示:
41
- *
42
- * 1 <= k <= 1000
43
- * 0 <= value <= 1000
44
- * insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000 次
45
- */
2
+ * [641] design circular deque
3
+ * https://leetcode.cn/problems/design-circular-deque/
4
+ *
5
+ * 设计实现双端队列。
6
+ *
7
+ * 实现 MyCircularDeque 类:
8
+ *
9
+ * MyCircularDeque(int k) :构造函数,双端队列最大为 k 。
10
+ * boolean insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true ,否则返回 false 。
11
+ * boolean insertLast() :将一个元素添加到双端队列尾部。如果操作成功返回 true ,否则返回 false 。
12
+ * boolean deleteFront() :从双端队列头部删除一个元素。 如果操作成功返回 true ,否则返回 false 。
13
+ * boolean deleteLast() :从双端队列尾部删除一个元素。如果操作成功返回 true ,否则返回 false 。
14
+ * int getFront() ):从双端队列头部获得一个元素。如果双端队列为空,返回 -1 。
15
+ * int getRear() :获得双端队列的最后一个元素。 如果双端队列为空,返回 -1 。
16
+ * boolean isEmpty() :若双端队列为空,则返回 true ,否则返回 false 。
17
+ * boolean isFull() :若双端队列满了,则返回 true ,否则返回 false 。
18
+ *
19
+ *
20
+ * 示例 1:
21
+ *
22
+ * 输入
23
+ * ["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
24
+ * [[3], [1], [2], [3], [4], [], [], [], [4], []]
25
+ * 输出
26
+ * [null, true, true, true, false, 2, true, true, true, 4]
27
+ *
28
+ * 解释
29
+ * MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
30
+ * circularDeque.insertLast(1); // 返回 true
31
+ * circularDeque.insertLast(2); // 返回 true
32
+ * circularDeque.insertFront(3); // 返回 true
33
+ * circularDeque.insertFront(4); // 已经满了,返回 false
34
+ * circularDeque.getRear(); // 返回 2
35
+ * circularDeque.isFull(); // 返回 true
36
+ * circularDeque.deleteLast(); // 返回 true
37
+ * circularDeque.insertFront(4); // 返回 true
38
+ * circularDeque.getFront(); // 返回 4
39
+
40
+ * 提示:
41
+ *
42
+ * 1 <= k <= 1000
43
+ * 0 <= value <= 1000
44
+ * insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000 次
45
+ */
46
46
struct MyCircularDeque {
47
47
data : Vec < i32 > ,
48
48
capacity : usize ,
49
49
front : usize ,
50
50
rear : usize ,
51
51
}
52
52
53
+ /**
54
+ * 1. 队列空一位可以用来判断队列满、空
55
+ * 2. 队列头指针和尾指针的移动是不同的,这里是根据题意头指针左移动后赋值,尾指针赋值后右移
56
+ * 3. 防止指针溢出,要对 self.capacity 取模,在 rust 里 (self.rear - 1 + self.capacity) % self.capacity 编译会报错
57
+ * 要写成 (self.rear + self.capacity + 1) % self.capacity 才行
58
+ * 4. Vec::with_capacity(capacity) 是有容量,但是 length 为 0,这时候用 data[0] 是赋值不了的,编译报错,一定要用 vec![0; capacity]
59
+ */
60
+
61
+ /**
62
+ * Your MyCircularDeque object will be instantiated and called as such:
63
+ * let obj = MyCircularDeque::new(k);
64
+ * let ret_1: bool = obj.insert_front(value);
65
+ * let ret_2: bool = obj.insert_last(value);
66
+ * let ret_3: bool = obj.delete_front();
67
+ * let ret_4: bool = obj.delete_last();
68
+ * let ret_5: i32 = obj.get_front();
69
+ * let ret_6: i32 = obj.get_rear();
70
+ * let ret_7: bool = obj.is_empty();
71
+ * let ret_8: bool = obj.is_full();
72
+ */
53
73
54
74
/**
55
75
* `&self` means the method takes an immutable reference.
56
76
* If you need a mutable reference, change it to `&mut self` instead.
57
77
*/
58
78
impl MyCircularDeque {
59
-
60
79
fn new ( k : i32 ) -> Self {
61
80
let capacity = ( k + 1 ) as usize ;
62
81
MyCircularDeque {
@@ -66,7 +85,7 @@ impl MyCircularDeque {
66
85
rear : 0 ,
67
86
}
68
87
}
69
-
88
+
70
89
fn insert_front ( & mut self , value : i32 ) -> bool {
71
90
if self . is_full ( ) {
72
91
return false ;
@@ -75,7 +94,7 @@ impl MyCircularDeque {
75
94
self . data [ self . front ] = value;
76
95
true
77
96
}
78
-
97
+
79
98
fn insert_last ( & mut self , value : i32 ) -> bool {
80
99
if self . is_full ( ) {
81
100
return false ;
@@ -84,64 +103,43 @@ impl MyCircularDeque {
84
103
self . rear = ( self . rear + 1 ) % self . capacity ;
85
104
true
86
105
}
87
-
106
+
88
107
fn delete_front ( & mut self ) -> bool {
89
108
if self . is_empty ( ) {
90
109
return false ;
91
110
}
92
111
self . front = ( self . front + 1 ) % self . capacity ;
93
112
true
94
113
}
95
-
114
+
96
115
fn delete_last ( & mut self ) -> bool {
97
116
if self . is_empty ( ) {
98
117
return false ;
99
118
}
100
119
self . rear = ( self . rear + self . capacity - 1 ) % self . capacity ;
101
120
true
102
121
}
103
-
122
+
104
123
fn get_front ( & self ) -> i32 {
105
124
if self . is_empty ( ) {
106
125
return -1 ;
107
126
}
108
127
self . data [ self . front ]
109
128
}
110
-
129
+
111
130
fn get_rear ( & self ) -> i32 {
112
131
if self . is_empty ( ) {
113
132
return -1 ;
114
133
}
115
134
let rear = ( self . rear + self . capacity - 1 ) % self . capacity ;
116
135
self . data [ rear]
117
136
}
118
-
137
+
119
138
fn is_empty ( & self ) -> bool {
120
139
self . front == self . rear
121
140
}
122
-
141
+
123
142
fn is_full ( & self ) -> bool {
124
143
self . front == ( self . rear + 1 ) % self . capacity
125
144
}
126
145
}
127
-
128
- /**
129
- * Your MyCircularDeque object will be instantiated and called as such:
130
- * let obj = MyCircularDeque::new(k);
131
- * let ret_1: bool = obj.insert_front(value);
132
- * let ret_2: bool = obj.insert_last(value);
133
- * let ret_3: bool = obj.delete_front();
134
- * let ret_4: bool = obj.delete_last();
135
- * let ret_5: i32 = obj.get_front();
136
- * let ret_6: i32 = obj.get_rear();
137
- * let ret_7: bool = obj.is_empty();
138
- * let ret_8: bool = obj.is_full();
139
- */
140
-
141
- /**
142
- * 1. 队列空一位可以用来判断队列满、空
143
- * 2. 队列头指针和尾指针的移动是不同的,这里是根据题意头指针左移动后赋值,尾指针赋值后右移
144
- * 3. 防止指针溢出,要对 self.capacity 取模,在 rust 里 (self.rear - 1 + self.capacity) % self.capacity 编译会报错
145
- * 要写成 (self.rear + self.capacity + 1) % self.capacity 才行
146
- * 4. Vec::with_capacity(capacity) 是有容量,但是 length 为 0,这时候用 data[0] 是赋值不了的,编译报错,一定要用 vec![0; capacity]
147
- */
0 commit comments