Skip to content

Commit 55ac251

Browse files
committed
Update 622 solution
1 parent bf4bbed commit 55ac251

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/solution/s0622_design_circular_queue.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
*/
3535

3636
struct MyCircularQueue {
37-
capcity: usize,
38-
length: usize,
3937
data: Vec<i32>,
38+
capcity: usize,
4039
front: usize,
4140
rear: usize,
4241
}
@@ -49,8 +48,8 @@ struct MyCircularQueue {
4948
impl MyCircularQueue {
5049

5150
fn new(k: i32) -> Self {
52-
let k = k as usize;
53-
MyCircularQueue { capcity: k, length: 0, data: vec![0; k], front: 0, rear: 0 }
51+
let k = k as usize + 1;
52+
MyCircularQueue { capcity: k, data: vec![0; k], front: 0, rear: 0 }
5453
}
5554

5655
fn en_queue(&mut self, value: i32) -> bool {
@@ -59,7 +58,6 @@ impl MyCircularQueue {
5958
}
6059
self.rear = (self.rear + 1) % self.capcity;
6160
self.data[self.rear] = value;
62-
self.length += 1;
6361
true
6462
}
6563

@@ -68,7 +66,6 @@ impl MyCircularQueue {
6866
return false;
6967
}
7068
self.front = (self.front + 1) % self.capcity;
71-
self.length -= 1;
7269
true
7370
}
7471

@@ -88,11 +85,11 @@ impl MyCircularQueue {
8885
}
8986

9087
fn is_empty(&self) -> bool {
91-
self.length == 0
88+
self.front == self.rear
9289
}
9390

9491
fn is_full(&self) -> bool {
95-
self.capcity == self.length
92+
self.front == (self.rear + 1) % self.capcity
9693
}
9794
}
9895

@@ -114,7 +111,6 @@ impl MyCircularQueue {
114111
#[test]
115112
fn test_622() {
116113
let mut queue = MyCircularQueue::new(3);
117-
assert_eq!(queue.capcity, 3);
118114
assert!(queue.en_queue(1));
119115
assert!(queue.en_queue(2));
120116
assert!(queue.en_queue(3));

0 commit comments

Comments
 (0)